问题
In my iPhone application, I have a secondary thread that I have made for performing tasks in the background, and every few milliseconds, the view must be updated (not from the main thread). I have tried the following code in my UIView subclass:
[self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];
That works great on the simulator, however, on my device, it is very laggy. The only thing I can think of is that the main thread is bogged up with the usual routines of the UIKit, meaning my setNeedsDisplay gets called long behind when it is supposed to (about 1/2 sec later). I think this because in other code, based off of user interaction, I call setNeedsDisplay right away, and it works fine without any lag at all. There are less backend computations on the secondary thread than there is on the user interface thread. This leads to some very bad visual effects in my application.
So, to sum things up, How can I fix this problem?
UPDATE:
Is there another way for me to tell a view it needs to refresh itself x times a second? that could end up being better performance for me, if that is possible. I have tried CADisplayLink with no success...
回答1:
If your UI is getting hit so hard that your call is getting delayed that much, try batching up the UI updates a little and/or doing beginUpdate/endUpdate around batches of TableView updates (for example.)
I am doing something similar in MonoTouch, and I'm not seeing any lag of that magnitude. All my updates are from background threads in the same manner as your app.
Alternatively if your main UI could use some more CPU time, try lowering the background thread priorities and see if that helps.
来源:https://stackoverflow.com/questions/5383399/uiview-setneedsdisplay-not-on-main-thread