UIView setNeedsDisplay Not on main thread?

耗尽温柔 提交于 2019-12-08 09:09:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!