Is there a way to build model objects on the background wire in AFNetworking?


I'm building an iOS application and I was wondering if there is a way to build my model objects from my request results on the background thread. It appears the success callbacks/blocks on AFHTTPClient request methods are all done on the main thread.

Use dispatch_async:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
        // Do model serialization here, e.g.:
        // NSMutableSet *mutableRecords = [NSMutableSet set];
        // for (NSDictionary *attributes in [JSON valueForKey:@"records"]) {
        //  [mutableRecords addObject:[[YourModelClass alloc] initWithDictionary:attributes]];
        // }

        dispatch_async(dispatch_get_main_queue(), ^(void) {
            // Execute callback passing, e.g.:
            // block(mutableRecords);

This would be in the success block of the request operation. The block variable would be the parameter passed into the method that made the request. See the iOS example project for more information. Hope that helps!