Re: Need help understanding a threading issue


Rick Aurbach
 

Okay, I've got it.

The problem was that I was calling OperationQueue.main.addOperation(..) from an Operation. And that call was both capturing "self" and was the last piece of work in the Operation's main(). So immediately after queuing the new main-thread operation, the caller was marked finished and was deleted. So by the time the main-thread operation executed, its attempt to capture "self" failed and (like all good closures) exited safely.

SOLUTION: class A is the controller for the process. It holds the operation queue as a property and sends it the addOperations([...]) call that begins the whole background process. One Operation wants to display status (doing UI on the main thread). In the working case, it does so by calling a method in class A which (in turn) issues the appropriate OperationQueue.main.addOperation{...} call. This way, when the main-thread operation actually runs, the "self" it captures is class A (which still exists) rather than the Operation (which no longer does).

Join cocoa@apple-dev.groups.io to automatically receive all group messages.