Imagine I have a spinner that I have to enable while something relatively heavy is being done and then deactivate the spinner after the task is done.
If I do:
[mySpinner startAnimating];
[self doSomethingHeavy];
[mySpinner stopAnimating];
I will never see the spinner running, because doSomethingHeavy will lock the thread and never let the spinner show.
I have tried to fire a new queue on the main thread using Grand Central Dispatch for the spinner and in another try for the task, but the results are the same. No spinner running.
The only way to make it work is to fire the method with a delay, using
[self performSelector:@selector(doSomethingHeavy) withObject:nil afterDelay:0.02];
but this sounds more like a hack and if I put [mySpinner stopAnimating] after that line, it will probably stop the spinner before the task is done.
This is not just valid for the spinner but for any task that that needs screen update.
I have tried these solutions but unfortunately none worked, because my doSomethingHeavy method must be run on the main thread too.
Using the hack of firing the method with a delay works but not for methods that should run with more than one parameter, as performSelector: afterDelay: cannot be used to pass more than one parameter.
I found a solution firing a queue on the main thread using Grand Central Dispatch and then sleeping it for 0.02 seconds. Using the queue, I can put anything I want.