Question

I am calling the setNeedsDisplay method in a new thread, but I don't see any changes in my view. What should I do to see all my changes after calling setNeedsDisplay in a new thread?

Was it helpful?

Solution

You can't update the user interface on a background thread. In your background thread, change

[object setNeedsDisplay];

to

[object performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];

OTHER TIPS

Any updates involving the UI must be made on the main thread. Generally, background threads are used for time-intensive tasks, such as downloading files, parsing data, etc...

Your main thread is responsible for updating the User Interface and responding to the users events and actions. That's the main reason we have background threads, to manage memory usage and to increase performance, by keeping the main thread as free as possible to respond to the user, while time-intensive tasks, which would normally block the main thread, happen in the background.

After you have processed all necessary data and information on your background thread, you must commit any changes to the UI according to your data by dispatching it to the main thread:

dispatch_async(dispatch_get_main_queue(), ^{
    //do UI stuff
});

Another way of dispatching to the main thread is as follows:

[self performSelectorOnMainThread:@selector(doUIStuff:) withObject:stuff waitUntilDone:NO];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top