This is the solution I've managed to find. I'm posting it in the case anyone is interested.
First I put all my 'multimedia actions' in a NSOperationQueue
and i set the maxConcurrentOperationCount
to 1, so I am certain that they will be run asynchronously from the main thread but only one at a time in a FIFO order (this is very important you you want that your content does not overlap on the screen).
Then for each action I activate it in a particular way depending on its class/kind. Let's suppose there are three kinds of actions:
- UIAlertViews subclasses
- MPMoviePlayerController-like
- Background actions (actions that do not involve multimedia content to appear instantly on the current view
For the first two I use NSNotifications
to manage the queue since I do not know when the content will stop playing or will be dismissed by the user (suppose by touch). So for each of those I:
- Pause the queue execution with
setSuspended:YES
- Add an observer for the corrisponding NSNotification
- Play the content
- Post a notification when they finish playing
- Remove the observer and start the queue again with
setSuspended:NO
For an UIAlertView the notification is posted with a performSelector:withObject:afterDelay:
saying the amount of time it shall be displayed or after a double tap. For A MPMovieController instead it is launched automatically and is named MPMoviePlayerPlaybackDidFinishNotification
. Both the UIAlertView and the MP kind shall be played on the main thread.
The third kind can be a set of instruction simply executed in the NSInvocationOperation put in the queue. So I can even create actions like 'wait' that suspend the queue eecution for some seconds