处理 NSNotificationCenter 通知导致 bad_access
我对应用程序的 UIWindow 进行子类化,然后当用户没有活动时,我使用 NSNotificationCenter 发出通知。我正在使用 [NSTimer SchedulerWithTimeInterval:target:selector: userInfo:repeats:]
我在 AppDelegate 类中处理这个问题。这工作正常,除非我使用 MBProgressHUD 运行代码 - http://iosdevelopertips.com/open-source/ios-open-source-heads-up-display-with-mbprogresshud.html 我认为这会启动另一个线程。当 Done 委托与对通知做出反应的代码运行同时触发时,就会发生错误。我很确定这是一个线程问题,但我该如何避免这种情况
I am subclassing the UIWindow of my app then when there is no activity from user I raise a notification using NSNotificationCenter. I am using
[NSTimer scheduledTimerWithTimeInterval:target:selector: userInfo:repeats:]
I handle this in my AppDelegate class. This works OK except when I am running code using MBProgressHUD - http://iosdevelopertips.com/open-source/ios-open-source-heads-up-display-with-mbprogresshud.html
which I think starts another thread. Error occurs when it's Done delegate fires the same time as when code that reacts to the Notification is running. I am pretty sure this is a threading issue but how do I avoid the situation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在应用程序委托上使用@synchronized。由于应用程序委托是一个单例,您必须由它保护关键部分:一个在“完成委托”中,另一个在您的应用程序委托中。
有关 Apple 线程文档的更多信息此处
编辑: 这个关于线程和通知的链接是一个很好的信息来源。
编辑2:这个更好
You could use @synchronized on app delegate. Since app delegate is a singletone you'll have to critical sections guarded by it: one in "done delegate" and another in your app delegate.
More info in apple threading documents here
EDIT: this link about threading and notifications is a good source of information for you.
EDIT 2: this one is even better