什么是runloop? [英] What is a runloop?

查看:121
本文介绍了什么是runloop?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读NSRunLoop的文档后,我不太了解。我生成一个次级线程,其中有一个NSTimer,它每1秒启动。哪个更新屏幕上的标签与performSelectorOnMainThread ..



然而,要得到它的工作我需要一个runloop但我不明白的概念吗?



任何人可以试着解释一下?



谢谢。

解决方案

运行循环是有效的:

  while ...)
... handle event ...;

主线程具有主事件循环,其中用户事件被处理并且大多数UI绘图等发生。 文档详细说明。 / p>

但是,在您的情况下,您不需要线程



听起来你所做的就是定期更新UI中的标签;



只需在主线程中安排计时器,并使用它完成计时。不需要使用 performSelectorOnMainThread:,或者承担所有的复杂性,保证线程之间的数据一致性。



< hr />

对不起 - 不明白你的问题。



在内部,在在这段时间过去之后,触发定时器的运行循环中。没有额外的线程涉及,更好的是,它不轮询检查时间。想一个运行循环有效地维护时间表。它会被动地让时间过去,直到在时间轴上发现了一些感兴趣的东西(所有没有轮询 - 轮询可以避免。)



这意味着,虽然,一个计时器永远不会100%准确。此外,如果您每隔一秒钟重复一次计时器,它就会随着时间而变化。



此外,而不是直接触发绘图事件。您的计时器应该使需要更新的视图无效,然后让底层对象处理最好实际更新屏幕的时间。


After reading the documentation for NSRunLoop I did not understand very much. I am spawning a secondary thread that has a NSTimer in it that launch every 1sec. Which update a label on the screen with the performSelectorOnMainThread..

However to get it to work I needed a runloop but I do not understand the concept of it?

Anyone who could try to explain it?

Thanks.

解决方案

A run loop is effectively:

while(... get an event ...)
    ... handle event ...;

It runs on a thread; the main thread has the main event loop where user events are processed and most UI drawing, etc, occurs. The documentation explains it in detail.

However, in your case, you don't need a thread.

It sounds like all you are doing is periodically updating a label in the UI; something that isn't terribly compute intensive.

Just schedule your timer in the main thread and be done with it. No need for spinning up a thread, using performSelectorOnMainThread:, or incurring all the complexities of guaranteeing data coherency across threads.


Sorry -- didn't understand your question.

Internally, a run loop works by basically putting a flag in the run loop that says "after this amount of time elapses, fire the timer". No additional threads involved and, better yet, it isn't polling to check the time. Think of a run loop as effectively maintaining a timeline. It'll passively let time elapse until there is something of interest found on the timeline (all without polling -- polling sucks. to be avoided.)

It does mean, though, that a Timer will never be 100% accurate. As well, if you have a timer repeating every second, it'll drift over time.

Also; instead of directly triggering a drawing event. Your timer should invalidate the view that needs updating, then let the underlying objects deal with when it is best to actually update the screen.

这篇关于什么是runloop?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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