If there is a view controller that wants to execute a number of block based tasks one at a time, how can the control be given back to the view controller after a completion block has executed.
Lets say ViewControllerOne wants to execute a number of tasks, each relying on the result of the previous one, how would this control be given back to the viewcontroller after each completion block has been executed?
I started thinking about this and I was heading towards a deeply nested block pattern that will surely only cause confusion to whoever else reads or tests it.
A task executes and the completion block returns the result or error which is needed by the next task, which has its own completion task, that the next task relies on and so forth.
How can the control be managed in one place, the viewcontroller?
Would the completion block just call the next function that handles the next task, using a pointer to the view controller(self) where it is being called?
Your comment about ‘giving control’ back to the viewController (VC) doesn’t sense, in that the VC is usually sitting blocked in the runLoop callback, waiting for something to happen.
If I understand what you want, its to have the opportunity to have other messages to the VC get processed while all this stuff is happening. Define a method:
Create all the blocks at once or do it leisurely, but add them to the blocks array. Each block gets one object passed to it which contains the work items it needs.:
The first block gets dispatched to some queue – could be a background queue too, and it sends its result back to the VC: