UIView setNeedsDisplay不是在主线程上吗? [英] UIView setNeedsDisplay Not on main thread?

查看:188
本文介绍了UIView setNeedsDisplay不是在主线程上吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPhone应用程序中,我有一个用于在后台执行任务的辅助线程,并且每隔几毫秒,必须更新一次视图(而不是从主线程).我在UIView子类中尝试了以下代码:

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];

这在模拟器上效果很好,但是,在我的设备上,它非常滞后.我唯一能想到的是主线程陷入了UIKit的常规例程,这意味着我的setNeedsDisplay应该在预期的时间被延迟很长时间(大约1/2秒后).我认为这是因为在其他代码中,基于用户交互,我立即调用了setNeedsDisplay,并且可以正常工作而没有任何滞后.辅助线程上的后端计算少于用户界面线程上的后端计算.这在我的应用程序中导致一些非常不好的视觉效果.

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?

更新:

我是否有另一种方法可以告诉视图它需要每秒刷新自身X次?如果可能的话,对我来说可能最终会获得更好的性能.我尝试了CADisplayLink,但没有成功...

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...

推荐答案

如果您的UI受到重击以至于您的通话被延迟那么多,请尝试对UI更新进行一点批处理和/或在周围进行beginUpdate/endUpdate批量的TableView更新(例如.)

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.)

我在MonoTouch中正在做类似的事情,但是我没有看到这么大的滞后.我所有的更新都来自后台线程,其方式与您的应用相同.

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.

或者,如果主UI可以使用更多的CPU时间,请尝试降低后台线程优先级,看看是否有帮助.

Alternatively if your main UI could use some more CPU time, try lowering the background thread priorities and see if that helps.

这篇关于UIView setNeedsDisplay不是在主线程上吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆