Inter thread 'communication' issues
In my app, I have two threads, first is the gui thread where I have code which is called when user presses each button.
In second thread I have video recording mechanics, where I can start, capture, append (record)video to the file.
Problem arises when I am trying to 'start recording' from first thread - it crashes. So I must set some kind of boolean 'communication flags' 开发者_Go百科in gui thread and then check them in video thread and then 'record video' from the video thread.
Such flags are bad I think - what is an easy and proper way to do that?
That's quite a broad topic you are touching there. Cocoa does contain some useful interthread communication methods though. You might want to look at
[NSObject performSelector:onThread:withObject:waitUntilDone:]
That will only work if you have a NSRunLoop running in the second thread. Otherwise have a bool @property in your video recording class. This will be the flag, that you suggested. Change its value from the main thread and check its value from the secondary thread in your recording loop to perform an action.
Don't forget that your @property declaration needs to be atomic.
You might also want to use NSOperationQueue
or blocks, it's hard to tell from your question what would work best. Depends on your coding preferences as well, I guess.
精彩评论