问题
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