Question

I am trying to implement RESTKit 0.20 operations queue, I have read on the blogs that NSOperationQueue may also be used to create queue of operations. I wants to use native approach of RestKit operations queue.

Can any one please post the piece of code/example with followings:

  • How to use Operations queue in RestKit.
  • Setting queue to execute one operation at a time.
  • If the first operation is not completed then I need to cancel pending operations in this queue.

Looks forward to hear form you.

Thanks.

Was it helpful?

Solution

Here I am sharing you piece of Code I am using for ManagedObjects(CoreData Objects) Request Operations.

Get references to objectManager & managedObjectContext;

RKObjectManager *objectManager = [(AppDelegate *)[[UIApplication sharedApplication] delegate] objectManager];
NSManagedObjectContext *managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];

Initialise array to hold up operations in it

NSMutableArray *requestOperations = [NSMutableArray array];

Prepare first Operation and add it to requestOperations array, notice failure block is cancelling pending operations in queue.

// 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];

prepare second operation

// 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];

prepare more operations

..

Set max concurrent operations count to 1

objectManager.operationQueue.maxConcurrentOperationCount = 1;

Enqueue all operations in queue. The queue will start executing operations one by one.

// Enqueue Request Operations
[objectManager enqueueBatchOfObjectRequestOperations:requestOperations progress: ^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
    NSLog(@"totalNumberOfOperations : %d", totalNumberOfOperations);
    NSLog(@"numberOfFinishedOperations : %d", numberOfFinishedOperations);
} completion: ^(NSArray *operations) {
    NSLog(@"completion");
}];

Hope that gonna deliver your purpose. Cheers,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top