2
votes

I am using AFNetworking in this example but I think it pertains more to NSOperation. I have two operations, once is dependent on the other finishing. However op2 really shouldn't run until op1's success block has completely run. In the case of dependencies in an operation queue, op2 will run as soon as op1 is done, but before op1's success block is done.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSURLRequest *request = [manager.requestSerializer requestWithMethod:@"GET" URLString:url parameters: nil error: nil];
NSOperation *op1 = [http.manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id userLocations) {
   NSLog(@"Success");
   // do some stuff 

   // more stuf

   // I am done, ready for the next operation.
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
   NSLog(@"Error: %@", error);
}];
NSOperation* op2 = // create op, this will depend on op1 finishing
[op2 addDependency:Op1];  // op2 is dependent on op1 finishing

[manager.operationQueue addOperations:@[op1, op2] waitUntilFinished:NO];

This does not quite work for me as op2 is dependent on some things that are set in op1's success block. Meaning op2 cannot start until op1 is done with its success block.

Is there a way with NSOperations to queue them such that each can wait until the blocks are done running as well? If not how can I re-design tho accomplish this dependency.

2
Could you use a dispatch semaphore? developer.apple.com/library/ios/documentation/General/…Paulw11
Dispatch is possible. Did not know about that, thanks I will take a look and see if I can solve my problem that way.lostintranslation

2 Answers

1
votes

I would structure things a bit differently, setting up the second operation within the first. Like this:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSURLRequest *request = [manager.requestSerializer requestWithMethod:@"GET" URLString:url parameters: nil error: nil];
NSOperation *op1 = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id userLocations) {
    NSLog(@"Success");
    // do some stuff

    // more stuf

    // I am done, ready for the next operation.
    // SO put the other operation here!
    NSOperation* op2 = // create op, this will depend on op1 finishing

    [manager.operationQueue addOperation:op2];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
[manager.operationQueue addOperation:op1];
0
votes

I run in to the same problem and found out an excellent solution with just using [operationQueue setSuspended:YES] please see the second answer in this post: NSOperation wait until asynchronous block executes