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

一笑奈何 提交于 2019-12-23 03:44:09

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/14821406/afurlconnectionoperation-start-method-gets-called-before-it-is-ready-and-never

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!