(iPhone)从后台线程启动计时器? [英] (iphone) start timer from background thread?

查看:75
本文介绍了(iPhone)从后台线程启动计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从后台线程使用[NSTimer scheduledTimerWithTimerInterval:target:selector:...]创建一个计时器
从后台线程创建的计时器似乎没有调用作为参数指定的选择器.

I create a timer with [NSTimer scheduledTimerWithTimerInterval:target:selector:...] from a background thread
It seems the timer created from a background thread doesn't call the selector given as the argument.

我需要从后台线程运行计时器吗?
谢谢

Is there something special I need to run a timer from background thread?
Thank you

  • 编辑

这是序列

performSelectorInBackground: pushViewController

从viewController的初始化序列

from the viewController's init sequence,

I alloc a timer with the above method.  

,并且不会调用计时器选择器.

and the timer selector won't get called.

推荐答案

您应该只从主线程对UI进行操作,例如推送视图控制器和更改UI项.如果不这样做,如您所见,事情就会破裂.

You should only do things to the UI - like pushing view controllers and changing UI items - from the main thread. If you don't, things break, as you can see.

请参阅标题为可可框架线程安全吗?"的可可基础指南"部分:内容为所有UIKit对象应仅在主线程上使用."

See the Cocoa Fundamentals Guide section titled "Are the Cocoa Frameworks Thread Safe?": it says "All UIKit objects should be used on the main thread only."

原始答案

没有什么特别的需要.选择器将在您从中调用scheduleTimer的线程上被调用.

There's nothing special needed. The selector will get called on the thread you did the scheduleTimer call from.

这篇关于(iPhone)从后台线程启动计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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