mutable.me
mutable.me

Reputation: 351

AFURLConnectionOperation 'start' method gets called before it is ready and never gets called again afterwards

We use AFNetworking (https://github.com/AFNetworking/AFNetworking) in our app and NSOperationStack (https://github.com/nicklockwood/NSOperationStack) to set dependencies so that last operation gets priority over the rest in the queue (stack behavior). I am running into an issue when 'start' method of AFURLConnectionOperation gets called, but operation's 'isReady' method returns NO because of the dependencies. This makes 'start' exit immediately. After the very first attempt to start operation, 'start' method never gets called again, thus the operation never reaches isFinished state, never gets deleted from the queue and eventually operations like that jam the queue. I would appreciate any thought on this matter. Thank you!

More info: none of the operations got cancelled and i don't see any dependency circles in the queue. We use setLIFODependendenciesForOperation on existing operationQueue not changing AFNetworking code: [self.operationQueue setLIFODependendenciesForOperation:operation];

Update: Now, thinking more about it, is it possible to have zero dependencies at one point and having isReady return YES when NSOperationQueue decides that operation is ready, but, by the time, start() gets invoked the number of dependencies changes to 1 or more.

Upvotes: 1

Views: 894

Answers (1)

Rory O'Bryan
Rory O'Bryan

Reputation: 1904

This sounds like an issue with the 'NS'OperationStack Github project that you are using.

Changing NSOperation dependancies after they have been added to the NSOperationQueue is specifically advised against in the documentation:

Important You should always configure dependencies before running your operations or adding them to an operation queue. Dependencies added afterward may not prevent a given operation object from running. (From: Concurrency Programming Guide: Configuring Interoperation Dependencies)

I think a better approach would be to maintain your own LIFO stack outside the operation queue and use the finishing of one operation to trigger the queuing of the next most important operation.

Or if you don't need to be strict perhaps you could use -[NSOperation setPriority:] to prioritise the later operations over the earlier ones.

Upvotes: 5

Related Questions