Should I call NSSavePanel runModal method only from main thread?

二次信任 提交于 2019-12-12 01:52:12

问题


I have weird, though rare, crashes in my application.

I suspect that it happens because runModal of NSSavePanel is called in a thread different from the main thread.

Am I correct that it might be the cause for the crashes?


回答1:


See the Threading Programming Guide from Apple:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html

"Thread-Unsafe Classes: NSWindow and all of its descendants."

NSSavePanel is a descendant of NSWindow.




回答2:


Yes.

I've had crashes calling runModal on the wrong thread. performSelectorOnMainThread is often useful to fix this.




回答3:


As a general rule, all access to UI elements should happen from the main thread, also sometimes referred to as the UI thread.

So I'd say yes, you should open it from the main thread.

As to whether that is the source of your crashes is hard to tell. My modest experience tells me that crashes can be caused by all sorts of things...



来源:https://stackoverflow.com/questions/6805697/should-i-call-nssavepanel-runmodal-method-only-from-main-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!