我正在尝试实现RESTKit 0.20操作队列,我已经阅读了NSOperationQueue可能也用于创建操作队列的博客.我想使用RestKit操作队列的本机方法.
任何人都可以发布以下代码/示例:
期待听到你的形象.
谢谢.
在这里,我将分享您用于ManagedObjects(CoreData Objects)请求操作的代码.
获取对objectManager和managedObjectContext的引用;
RKObjectManager *objectManager = [(AppDelegate *)[[UIApplication sharedApplication] delegate] objectManager];
NSManagedObjectContext *managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
Run Code Online (Sandbox Code Playgroud)
初始化数组以保持其中的操作
NSMutableArray *requestOperations = [NSMutableArray array];
Run Code Online (Sandbox Code Playgroud)
首先准备操作并将其添加到requestOperations数组,注意故障块正在取消队列中的待处理操作.
// Setup Organization Operation
//
NSString *url = @"organizations/syncAll/";
NSMutableURLRequest *organizationsRequest = [objectManager requestWithObject:organizations method:RKRequestMethodPOST path:url parameters:nil];
RKObjectRequestOperation *organizationsOperation = [objectManager managedObjectRequestOperationWithRequest:organizationsRequest managedObjectContext:managedObjectContext success: ^(RKObjectRequestOperation *operation, RKMappingResult *result) {
..
[RKUtils isHandleStatusError:[result array]];
} failure: ^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Failed with error: %@", [error localizedDescription]);
[objectManager cancelAllObjectRequestOperationsWithMethod:RKRequestMethodPOST matchingPathPattern:@"games/getNotStartedGames"];
[RKUtils handleError:error];
}];
[requestOperations addObject:organizationsOperation];
Run Code Online (Sandbox Code Playgroud)
准备第二次手术
// Setup Games Operation
//
url = @"games/syncAll/";
NSMutableURLRequest *gamesRequest = [objectManager requestWithObject:games method:RKRequestMethodPOST path:url parameters:nil];
RKObjectRequestOperation *gamesOperation = [objectManager managedObjectRequestOperationWithRequest:gamesRequest managedObjectContext:managedObjectContext success: ^(RKObjectRequestOperation *operation, RKMappingResult *result) {
..
[RKUtils isHandleStatusError:[result array]];
} failure: ^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Failed with error: %@", [error localizedDescription]);
if (error.code == NSURLErrorCancelled) {
return;
}
[RKUtils handleError:error];
}];
[requestOperations addObject:gamesOperation];
Run Code Online (Sandbox Code Playgroud)
准备更多的业务
..
Run Code Online (Sandbox Code Playgroud)
将最大并发操作数设置为1
objectManager.operationQueue.maxConcurrentOperationCount = 1;
Run Code Online (Sandbox Code Playgroud)
将所有操作排入队列.队列将逐个开始执行操作.
// Enqueue Request Operations
[objectManager enqueueBatchOfObjectRequestOperations:requestOperations progress: ^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"totalNumberOfOperations : %d", totalNumberOfOperations);
NSLog(@"numberOfFinishedOperations : %d", numberOfFinishedOperations);
} completion: ^(NSArray *operations) {
NSLog(@"completion");
}];
Run Code Online (Sandbox Code Playgroud)
希望能实现你的目的.干杯,
| 归档时间: |
|
| 查看次数: |
1050 次 |
| 最近记录: |