diff --git a/AlamofireObjectMapper/AlmofireObjectMapper.swift b/AlamofireObjectMapper/AlmofireObjectMapper.swift index bf9a861..ffd410f 100644 --- a/AlamofireObjectMapper/AlmofireObjectMapper.swift +++ b/AlamofireObjectMapper/AlmofireObjectMapper.swift @@ -20,16 +20,8 @@ extension Request { :returns: The request. */ public func responseObject(completionHandler: (T?, NSError?) -> Void) -> Self { - return response(queue: nil, serializer: Request.JSONResponseSerializer(options: NSJSONReadingOptions.AllowFragments)) { (request, response, object, error) -> Void in - - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { - - let parsedObject = Mapper().map(object) - - dispatch_async(dispatch_get_main_queue()) { - completionHandler(parsedObject, error) - } - } + return responseObject(nil) { (request, response, object, data, error) -> Void in + completionHandler(object, error) } } @@ -65,6 +57,54 @@ extension Request { } } } + } + + // MARK: Array responses + + /** + Adds a handler to be called once the request has finished. + + :param: completionHandler A closure to be executed once the request has finished and the data has been mapped to a swift Object. The closure takes 2 arguments: the response array (of type Mappable) and any error produced while making the request + + :returns: The request. + */ + public func responseArray(completionHandler: ([T]?, NSError?) -> Void) -> Self { + return responseArray(nil) { (request, response, object, data, error) -> Void in + completionHandler(object, error) + } + } + + /** + Adds a handler to be called once the request has finished. + + :param: completionHandler A closure to be executed once the request has finished and the data has been mapped to a swift Object. The closure takes 5 arguments: the URL request, the URL response, the response array (of type Mappable), the raw response data, and any error produced making the request. + + :returns: The request. + */ + public func responseArray(completionHandler: (NSURLRequest, NSHTTPURLResponse?, [T]?, AnyObject?, NSError?) -> Void) -> Self { + return responseArray(nil, completionHandler: completionHandler) + } + + /** + Adds a handler to be called once the request has finished. + + :param: queue The queue on which the completion handler is dispatched. + :param: completionHandler A closure to be executed once the request has finished and the data has been mapped to a swift Object. The closure takes 5 arguments: the URL request, the URL response, the response array (of type Mappable), the raw response data, and any error produced making the request. + + :returns: The request. + */ + public func responseArray(queue: dispatch_queue_t?, completionHandler: (NSURLRequest, NSHTTPURLResponse?, [T]?, AnyObject?, NSError?) -> Void) -> Self { + return response(queue: queue, serializer: Request.JSONResponseSerializer(options: NSJSONReadingOptions.AllowFragments)) { (request, response, data, error) -> Void in + + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { + + let parsedObject = Mapper().mapArray(data) + + dispatch_async(queue ?? dispatch_get_main_queue()) { + completionHandler(self.request, self.response, parsedObject, data, error) + } + } + } } } \ No newline at end of file diff --git a/AlamofireObjectMapperTests/AlamofireObjectMapperTests.swift b/AlamofireObjectMapperTests/AlamofireObjectMapperTests.swift index f07ed15..ee911a3 100644 --- a/AlamofireObjectMapperTests/AlamofireObjectMapperTests.swift +++ b/AlamofireObjectMapperTests/AlamofireObjectMapperTests.swift @@ -73,6 +73,50 @@ class AlamofireObjectMapperTests: XCTestCase { XCTAssertNil(error, "\(error)") }) } + + func testArrayResponseObject() { + // This is an example of a functional test case. + let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/f583be1121dbc5e9b0381b3017718a70c31054f7/sample_array_json" + let expectation = expectationWithDescription("\(URL)") + + Alamofire.request(.GET, URL, parameters: nil).responseArray { (response: [Forecast]?, error: NSError?) in + expectation.fulfill() + + XCTAssertNotNil(response, "Response should not be nil") + + for forecast in response! { + XCTAssertNotNil(forecast.day, "day should not be nil") + XCTAssertNotNil(forecast.conditions, "conditions should not be nil") + XCTAssertNotNil(forecast.temperature, "temperature should not be nil") + } + } + + waitForExpectationsWithTimeout(10, handler: { (error: NSError!) -> Void in + XCTAssertNil(error, "\(error)") + }) + } + + func testArrayResponseObject2() { + // This is an example of a functional test case. + let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/f583be1121dbc5e9b0381b3017718a70c31054f7/sample_array_json" + let expectation = expectationWithDescription("\(URL)") + + Alamofire.request(.GET, URL, parameters: nil).responseArray { (request: NSURLRequest, HTTPURLResponse: NSHTTPURLResponse?, response: [Forecast]?, data: AnyObject?, error: NSError?) in + expectation.fulfill() + + XCTAssertNotNil(response, "Response should not be nil") + + for forecast in response! { + XCTAssertNotNil(forecast.day, "day should not be nil") + XCTAssertNotNil(forecast.conditions, "conditions should not be nil") + XCTAssertNotNil(forecast.temperature, "temperature should not be nil") + } + } + + waitForExpectationsWithTimeout(10, handler: { (error: NSError!) -> Void in + XCTAssertNil(error, "\(error)") + }) + } } class WeatherResponse: Mappable {