开发者

iPhone - start two processes simultaneously

I have to start more than one processes simultaneously - an AVAudioPlayer's play, a timer and a file write operation.

If I just write them in a method, they are executed one afte开发者_如何学Pythonr the other and the start of all the operations has a slight lag.

Any ideas? Appreciate any help.

Thanks.


Why don't you use threads?

You can do it easily by using:

- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg

or by using NSOperation with a NSOperationQueue.

Have a look at this question for more details.

Notes:

  • you'll need an autorelease pool in your thread if you use any autoreleased classes
  • if you want to use a timer in another thread you'll also need another runloop.
  • watch out for thread safetiness. For instance, I don't know if AVAudioPlayer can be used on a thread different from the main thread.
  • I don't think you can schedule a NSTimer from within a NSOperation


Adding too much concurrency could be the wrong approach here. You might want to investigate setting up the timer and playing the audio file on the main thread while performing the blocking file write call on a background thread. The important concept here is to stay away from blocking calls on the main thread, especially when you're dealing with file or network I/O.

You'll want to avoid creating too many extra threads as the iPhone is a single-CPU device, and multiple threads will cause more resource consumption.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜