0
我在後臺線程中執行getObjectsAtPath。但是,成功/失敗塊在UI線程上被調用。Reskit - 回調在同一個線程而不是UI線程
有沒有辦法強制RestKit調用同一線程上的成功和失敗塊,而不是在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);