evermeer / AlamofireJsonToObjects

An Alamofire extension which converts JSON response data into swift objects using EVReflection
Other
161 stars 28 forks source link

Improve Request extension #2

Closed kukudas closed 9 years ago

kukudas commented 9 years ago

I'm having a generic function which fires requests to different endpoints. Sometimes these endpoints do not return anything. This is expected. I saw that the responseObject method will fail if the data object is nil.

My proposal would be to change it like this:

 public func responseObject<T:EVObject>(queue: dispatch_queue_t?, completionHandler: (NSURLRequest, NSHTTPURLResponse?, T?, AnyObject?, NSError?) -> Void) -> Self {
        return responseString{(request, response, data, error) in
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
                var parsedObject:T?
                if let notEmptyData = data {
                  parsedObject = T(json: notNilObject)
                }
                dispatch_async(queue ?? dispatch_get_main_queue()) {
                    completionHandler(self.request, self.response, parsedObject, data, error)
                }
            }

        }
    }

Let me know what you think about this. For the responseArray function something similar is needed.

evermeer commented 9 years ago

You are right, this should be handled. But I prefer solving it lower level. I am currently looking at how I should fix this in EVReflection.

evermeer commented 9 years ago

I just pushed EVReflection 2.4.5 plus AlamofireJsonToObjects 1.0.1 to GitHub. please run a 'pod update' and let me know if you find any other issues.

kukudas commented 9 years ago

Thank you!