PerformSelectorOnMainThread 不执行

发布于 2024-12-12 10:07:39 字数 919 浏览 0 评论 0原文

对此我感到很烦恼,任何想法或建议将不胜感激。

我有一个对象从子线程调用 performSelectorOnMainThread:withObject:waitUntilDone: 。这在 95% 的情况下都可以正常工作。自 iOS 测试版到现在发布以来,设备偶尔会拒绝执行 performSelectorOnMainThread:withObject:waitUntilDone: 调用...没有错误消息,它不会崩溃,我可以' t 让设备进入“失败”状态,但一旦到达那里,它就会继续失败,直到我删除并重新安装该应用程序,或强制其退出,然后调整其位置服务限额,然后再次启动它,然后重新调整其位置服务使其恢复正常...重新启动设备并不能解决问题。不先删除而重新安装并不能解决问题。这很奇怪......我知道它在大多数情况下都可以工作,因为大多数设备都没有问题,但是有些设备会定期出现故障(可能每 3 或 4 天一次)。我知道具体来说, performSelectorOnMainThread:withObject:waitUntilDone: 没有调用它应该调用的内容,因为我现在有一个失败的设备,并且我已将 NSLog 放入应该调用的方法中。它运行良好,但在出现故障的设备上,当使用 performSelectorOnMainThread:withObject:waitUntilDone: 调用该方法时,NSLog 显示它没有运行...

这种情况从 iOS 5 beta 开始发生,并且再次发生,发生在释放时。这种情况最常发生在我的 2 台设备上,但在我亲自测试过的其他 10 台设备上都没有发生。我以为这只是我的设备在测试版中出现的一些问题,但它发生在我从未接触过测试版的全新 4S 上,以及一位用户的 iPad 2 上(不是我的 iPad 2)。

我真的不知道该去哪里看。我告诉它执行,它通常在几乎所有设备上执行,但同一行在某些设备上没有响应,也没有错误等......

Tearing my hair out on this, any thoughts or suggestions would be greatly appreciated.

I have an object that calls performSelectorOnMainThread:withObject:waitUntilDone: on itself from a child thread. This works fine 95% of the time. Every once in a while since iOS betas and now into release, a device will refuse to execute the performSelectorOnMainThread:withObject:waitUntilDone: calls... No error message, it doesn't crash, I can't make a device go into a state where it "fails" but once it's there it continues to fail until I either delete and re-install the app, or force it to quit, then adjust it's location services allowance, then launch it again, then re-adjust it's location services again back to normal... restarting the device does not fix it. Re-installing without deleting first doesn't fix it. It's very strange.... I know it works most of the time because most devices have no problems, however some devices fail somewhat regularly (every 3 or 4 days maybe). I know it's specifically that performSelectorOnMainThread:withObject:waitUntilDone: is not calling what it should because I've got a failing device now, and I've put an NSLog into the method that should be called. It function fine, but on the failing device, when performSelectorOnMainThread:withObject:waitUntilDone: is used to call that method the NSLog shows it's not being run...

This started happening with iOS 5 betas and again, happens on release. It happens most often on 2 of my devices, but on none of the other 10 devices I have personally tested on. I assumed it was just my device from some hiccup in the beta, but it happens on my brand new 4S which never touched beta, as well as on one user's iPad 2 (not on my iPad 2).

I really don't know where to look. I tell it to execute and it usually does on almost every device, but the same line gets no response and no errors etc. on some...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏雪无痕 2024-12-19 10:07:40

PerformSelectorOnMainThread:withObject:waitUntilDone 有时可能会很不稳定。您是否考虑过尝试使用 lib 调度?

您可以创建一个块并将其发布到主线程上,如下所示:

dispatch_async(dispatch_get_main_queue(), ^{
    <do work here>
});

这与使用performSelectorOnMainThread:withObject:waitUntilDone具有相同的保存效果

http://developer.apple.com/library/ios/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW1

performSelectorOnMainThread:withObject:waitUntilDone can be wonky sometimes. Have you thought about trying to use lib dispatch?

You can create a block and post it on the main thread like this:

dispatch_async(dispatch_get_main_queue(), ^{
    <do work here>
});

This would have the same save affect as using performSelectorOnMainThread:withObject:waitUntilDone

http://developer.apple.com/library/ios/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW1

漆黑的白昼 2024-12-19 10:07:40

线程的 RunLoop 有几种不同的模式。
在您进行调用期间,主线程可能以不同于默认模式的模式运行(这是 performSelectorOnMainThread:withObject:WaitUntilDone: 的目标模式。

请考虑使用 PerformSelectorOnMainThread:withObject:waitUntilDone:modes:
请参阅 Apple 文档

另外 - GCD (libdispatch) 很棒,但它不能保护你免于陷入僵局:
考虑您的方法在主线程上运行并且您正在调用的情况:

dispatch_sync(dispatch_get_main_queue(), ^{
    <do work here>
});

A Thread's RunLoop has several different modes.
It is possible that during the time you make your call the main thread is running in a mode different than the default one (which is the one targeted by performSelectorOnMainThread:withObject:WaitUntilDone:.

Consider using performSelectorOnMainThread:withObject:waitUntilDone:modes:
See Apple's documentation.

Also - GCD (libdispatch) is awesome, but it won't protect you from cooking yourself a deadlock:
Consider the case where your method is running on the main thread and you're calling:

dispatch_sync(dispatch_get_main_queue(), ^{
    <do work here>
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文