I have a multithreaded program that needs to run many executables at once and wait for their results.
I use [nstask waitUntilExit]
in an NSOperationQueue
that runs it on non-main thread (running NSTask
on the main thread is completely out of the question).
My program randomly crashes or runs into assertion failures, and the crash stacks always point to the runloop run by waitUntilExit
, which executes various callbacks and handlers, including—IMHO incorrectly—KVO and bindings updating the UI, which causes them to run on non-main thread (It's probably the problem described by Mike Ash)
How can I safely use waitUntilExit
?
Is it a problem of waitUntilExit
being essentially unusable, or do I need to do something special (apart from explicitly scheduling my callbacks on the main thread) when using KVO and IB bindings to prevent them from being handled on a wrong thread running waitUntilExit
?
-waitUntilExit
? Why not launch the task from the main thread but, rather than blocking waiting for it to exit, add an observer forNSTaskDidTerminateNotification
and do whatever work you want there? You will have to maintain a strong reference to the task object for the duration, but that's easy enough. – FidgetNSOperation
is easier to manage than async one. With events and callbacks my ObjC code starts to look like node.js. – CyntheawaitUntilExit
only ifisRunning
is true. Solved my crashes – Hertha