Search code examples
iosios8restkit

Reskit - callback on the same thread instead of UI thread


I am performing getObjectsAtPath in a background thread. However, the success/failure blocks are called on the UI thread.

Is there a way to force RestKit to call success and failure blocks on the same thread instead on the UI thread?


Solution

  • Restkit does not provide this functionality, but you can archive this.

    RKObjectRequestOperation class has two properties successCallbackQueue & failureCallbackQueue, which are allows you to set call back queue. Overwrite RKObjectManager class and return RKObjectRequestOperation then you can set callback queues.

    - (RKObjectRequestOperation *)getObjectsAtPath:(NSString *)path
              parameters:(NSDictionary *)parameters
                 success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
                 failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure
    {
        NSParameterAssert(path);
        RKObjectRequestOperation *operation = [self appropriateObjectRequestOperationWithObject:nil method:RKRequestMethodGET path:path parameters:parameters];
        [operation setCompletionBlockWithSuccess:success failure:failure];
        [self enqueueObjectRequestOperation:operation];
        return operation;
    }
    

    Then you can set callback queues as shown bellow:

    RKObjectManager *objectManager = [RKObjectManager sharedManager];
    RKObjectRequestOperation *operation = [objectManager getObjectsAtPath:path
                         parameters:parameters
                            success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    
                            }
                            failure:^(RKObjectRequestOperation *operation, NSError *error) {
    
                            }];
    
    operation.successCallbackQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
    operation.failureCallbackQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);