Duck
Duck

Reputation: 35953

Doing a task that needs to update the screen

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.

Upvotes: 1

Views: 71

Answers (3)

Duck
Duck

Reputation: 35953

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.

Upvotes: 0

John Lemberger
John Lemberger

Reputation: 2699

    ...
    [mySpinner startAnimating];
    [self performSelectorInBackground:@selector(doSomethingHeavy) withObject:nil];
    ...
}


- (void)doSomethingHeavy {
   ...

   [mySpinner performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
}

Or instead of stopping the spinner in doSomethingHeavy it would more likley finish with a call to:

[self performSelectorOnMainThread:@selector(finishedSomethingHeavy) withObject:nil waitUntilDone:NO];

which would stop the spinner and update the UI with the heavy results.

Upvotes: 4

Sam
Sam

Reputation: 839

Hopefully I'm understanding the question properly, but normally I'd use the method

[self performSelectorInBackground:@selector(doSomethingHeavy) withObject:nil];

in this situation as this leaves the main thread free to update the UI while still performing the task in the background.

Upvotes: 0

Related Questions