Reskit - 回调在同一个线程而不是UI线程
问题描述:
我在后台线程中执行getObjectsAtPath。但是,成功/失败块在UI线程上被调用。Reskit - 回调在同一个线程而不是UI线程
有没有办法强制RestKit调用同一线程上的成功和失败块,而不是在UI线程上?
答
Restkit不提供此功能,但可以将其归档。
RKObjectRequestOperation
类有两个属性successCallbackQueue
& failureCallbackQueue
,它们允许你设置回调队列。覆盖RKObjectManager
类并返回RKObjectRequestOperation
然后您可以设置回调队列。
- (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;
}
那么你可以设置回调队列为波纹管所示:
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);