- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于我正在开发的游戏,我从触摸处理例程之一调用了一种昂贵的方法。为了使其更快,我决定使用 performSelectorInBackgroundThread
,因此改为:
[gameModel processPendingNotifications];
我切换到:
[gameModel performSelectorInBackground:@selector(processPendingNotifications) withObject:nil];
我遇到的第一个问题是 processPendingNotifications
没有 NSRunLoop
,所以我添加了它,如下所示:
- (void)processPendingNotifications {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[pendingNotificationsQueue makeObjectsPerformSelector:@selector(main)];
[pendingNotificationsQueue removeAllObjects];
[pool drain];
}
到目前为止一切顺利。我的问题是,从此后台线程调用的某些方法会创建新的 NSTimer 实例。这些实例最终不会被触发。我认为这是因为我拥有的辅助线程没有(或正在结束它的)NSRunLoop
。我正在使用以下方法启动新计时器:
[NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(timerFired) userInfo:nil repeats:NO];
我的问题是:
NSRunLoop
有关?NSTimer
并将其附加到主线程的 NSRunLoop
?最佳答案
是的,您需要一个运行循环来调度计时器事件
NSTimers 隐式附加到创建它们的线程的运行循环,因此如果您希望将其附加到主线程的运行循环,请使用 PerformSelectorOnMainThread:withObject:waitUntilDone: 在主线程上创建它。当然,该代码将在主线程上执行。
如果您的问题是“我可以在主线程的运行循环上运行一个计时器,直接在另一个线程上运行选择器吗?”答案是否定的,但它在主线程上触发的选择器可以执行Selector:onThread:withObject:waitUntilDone:。当然,这需要您尝试执行选择器的线程有一个可操作的运行循环。
如果您希望计时器触发的代码在后台线程上运行,那么您确实需要让后台线程的运行循环运行,如果您这样做,那么您不需要在主线程中安排任何内容,因为您将有一个可操作的运行循环。
关于iphone - PerformSelectorInBackgroundThread 和 NSTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1201884/
注意:我在这个项目中使用 swift。 我目前正在开发我的第一个项目,如果在一段时间后没有按下按钮,我会尝试停止(无效)NSTimer。我找到了其他停止 NSTimer 的方法,但没有说明如何在一定时
我正在 Xcode4 中工作,在一个项目中,出于组织目的,我一直使用三个不同的 NSTimer,但我意识到,除了名称之外,计时器是相同的,因此很容易将所有三个计时器合并为一。对于我的项目来说,哪一个更
根据 Apple 文档,将同时使用 WKInterfaceTimer(Watch 本地,倒计时但在结束时不触发任何事件)和 NSTimer(在计时器结束时触发方法)。所以,我的 App Interfa
我目前正在尝试制作一个 queueHandler,它将一个对象数组作为输入,用于在一个简单的 Double 机器人上执行驱动命令。我目前正在尝试使用 GCD 以串行执行我的函数,但是当我在我的队列中使
有个问题...我有定时器 [NSTimer scheduledTimerWithTimeInterval:120
我创建了一个计时器, self.scanTimer = [NSTimer scheduledTimerWithTimeInterval:TIMEOUT_SECONDS target:self s
我尝试使用以下代码停止 NSTimer: - (void)viewDidLoad { [super viewDidLoad]; timer3 = [NSTimer timerWithT
我正在制作一个带有计时器的应用程序。我从给定时间开始计算分钟和秒,直到 0。发生这种情况时,我启动一个警报 View 。 我的结构是这样的: 主线程方法分配一个新线程并初始化它。线程的入口点(方法)有
好吧,这段代码非常基本。用户将答案输入文本框,如果等于“第一+第二”,他们就得到一分。然后,他们有 5 秒钟的时间回答下一个数学问题。如果他们这样做了,函数“doCalculation”将再次运行,他
我正在创建益智游戏应用程序,并使用 NSTimer 显示时间(即 01:20)。当应用程序进入后台时,NSTimer 会暂停,但即使应用程序处于后台状态,我也想继续它。 例如,当应用程序进入后台时,计
我有一个带有 NSTimer *myTimer; 变量的类。在某些时候我会这样做: myTimer = [NSTimer scheduledTimerWithTimeInterval:20 targe
Edit2:为什么在“doSomething”方法中只更新了进度而不是point0? 编辑:使用我拥有的代码。我知道我一定忽略了一些东西,但我就是找不到它。 我正在编写一个 iPhone 应用程序,它
我的应用是一款体育应用。即使在后台游戏也应该继续,我为此使用了 nstimer。但我无法在后台运行计时器。每当应用程序处于后台时,计时器就会停止,返回时它会从离开时的同一时间继续。 最佳答案 您无法在
它将不断倒计时并更新当前时间。任何人都知道如何解决这个问题?我不明白 NStimer 如何识别这样的日期格式:20110803 23:59:59 最佳答案 查找NSDateFormatter 。您可以
是否可以在主线程上运行由 nstimer 调用的选择器? NSTimer 在它自己的线程中生成。 我的结构是一个线程调用一个带有 nstimer 的方法,nstimer 调用一个执行一些更新的方法,但
我在我的应用程序中使用了很多计时器。用于记录时间、移动物体、淡入淡出等。我在不同时间、同一 View 中使用同一个计时器来实现多个目的。我应该如何正确声明和无效或释放我的计时器? Atm 我这样声明计
我正在尝试使用 iPhone SDK 3.0 在线程上运行 NSTimer。我认为我做的一切都是正确的(新的运行循环等)。如果我在 viewDidDissappear 上调用 [timer inval
这是让我困惑了一段时间的事情。 我有一个 NSTimer,添加到 currentRunLoop 中,如果我不保留它,它就会崩溃。 NSTimer *timer = [[NSTimer timerWit
当我们使用 NSTimer 时,一旦在上述时间间隔后调用回调,UI 是否会被阻塞? 最佳答案 这要看情况。大多数时候,这不会成为问题。 如果,但是,满足以下两个条件,NSTimer 将阻塞 UI 线程
我使用大约 20 个 UIImageView 的小型飞机图像(50x50 像素)在 iPhone 屏幕上制作简单的动画。动画是通过按计时器间隔移动 UIImageView center 属性来完成的。
我是一名优秀的程序员,十分优秀!