3

I need to perform a series of server calls that run sequentially and where one request can only be executed if all previous requests have been successful.

So, my idea was to create an AFHTTPRequestOperation for each request and use [myAFHTTPClient enqueueBatchOfHTTPRequestOperations:] to fire them off.

I can make them run sequentially by calling
[myAFHTTPClient.operationQueue setMaxConcurrentOperationCount:1]

But how can I make sure that the remaining operations run only if the previous operations were successfull?

I tried to create a completionBlock for every operation that calls [myAFHTTPClient cancelAllOperations] in case the operation failed, but the completionBlock and the next operation in the queue run concurrently, so the next request could already be sent to the server before it gets cancelled. What should I do?

Tom
  • 260
  • 4
  • 13
  • Checking in here to see if you made any progress? I'm about to start work on something very similar to this. – jer-k Oct 17 '12 at 17:19
  • 2
    Since AFHTTPRequestOperations are just standard NSOperations, wrote a sample project and figured out how to solve this problem. See my answer on this question for an explanation: http://stackoverflow.com/questions/11413156/do-nsoperations-and-their-completionblocks-run-concurrently – Tom Nov 20 '12 at 17:02
  • @Tom you should post your comment as an actual answer. – Mike Sukmanowsky Dec 17 '13 at 02:26
  • @MikeSukmanowsky Thanks for the tip, I posted the answer. – Tom Mar 04 '14 at 12:38

1 Answers1

7

Since AFHTTPRequestOperations are just standard NSOperations, wrote a sample project and figured out how to solve this problem:

If the NSOperationQueue's maxConcurrentOperationCount is set to 1, an NSOperation's completionBlock and the next NSOperation in the queue run simultaneously.

But, if every NSOperation is linked to its previous operation by calling addDependency:, the execution of an operation waits until the previous operation's completionBlock has finished.

So, link all NSOperations together via addDependency: and in case an operation failes, cancel the remaining operations in the completion block of the current operation.

(see also Do NSOperations and their completionBlocks run concurrently?)

Community
  • 1
  • 1
Tom
  • 260
  • 4
  • 13