iphone - PerformSelectorInBackgroundThread 和 NSTimer

标签 iphone objective-c multithreading

对于我正在开发的游戏,我从触摸处理例程之一调用了一种昂贵的方法。为了使其更快,我决定使用 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];

我的问题是:

  1. 我是否怀疑问题与 NSRunLoop 有关?
  2. 有没有办法从后台线程启动 NSTimer 并将其附加到主线程的 NSRunLoop

最佳答案

是的,您需要一个运行循环来调度计时器事件

NSTimers 隐式附加到创建它们的线程的运行循环,因此如果您希望将其附加到主线程的运行循环,请使用 PerformSelectorOnMainThread:withObject:waitUntilDone: 在主线程上创建它。当然,该代码将在主线程上执行。

如果您的问题是“我可以在主线程的运行循环上运行一个计时器,直接在另一个线程上运行选择器吗?”答案是否定的,但它在主线程上触发的选择器可以执行Selector:onThread:withObject:waitUntilDone:。当然,这需要您尝试执行选择器的线程有一个可操作的运行循环。

如果您希望计时器触发的代码在后台线程上运行,那么您确实需要让后台线程的运行循环运行,如果您这样做,那么您不需要在主线程中安排任何内容,因为您将有一个可操作的运行循环。

关于iphone - PerformSelectorInBackgroundThread 和 NSTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1201884/

相关文章:

ios - 类型 'PHPhotoLibrary' 的值没有成员 'presentLimitedLibraryPicker' - Xcode 11.3.1

iphone - NSNumberFormatter-Decimal Style-格式化无限字符

ios - UICollectionview 和 Image loading 导致内存不足

objective-c - 如何在 Cocoa 应用程序中存储敏感数据(例如密码、API key )?

java - N个线程的多线程

iOS 7 蓝牙输出不适用于 AVAudioSessionCategoryPlayAndRecord

iphone - 使用 objective-c 从 Microsoft Exchange Server 读取邮件和文件夹列表

ios - 如何在 swift 中集成 PayU Money

.net - 这段代码是线程安全的吗?

java - 使用线程监控HashMap更新