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?

- 7,529
- 6
- 44
- 51

- 19
- 5
-
3All UI updates must be called on the main thread. – rmaddy May 10 '14 at 16:02
-
4Multi-threading does work. Any chance you can provide a more descriptive caption? – JensG May 10 '14 at 16:22
-
possible duplicate of [What Needs To Be on Main Thread?](http://stackoverflow.com/questions/8537979/what-needs-to-be-on-main-thread) – JensG May 10 '14 at 20:45
2 Answers
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];

- 3,507
- 2
- 21
- 26

- 7,529
- 6
- 44
- 51
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];

- 3,507
- 2
- 21
- 26