iPhone - 同时启动两个进程
我必须同时启动多个进程 - AVAudioPlayer 的播放、计时器和文件写入操作。
如果我只是将它们写在一个方法中,它们会被一个接一个地执行,并且所有操作的开始都会有轻微的滞后。
有什么想法吗?感谢任何帮助。
谢谢。
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 after the other and the start of all the operations has a slight lag.
Any ideas? Appreciate any help.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用线程呢?
您可以通过使用以下方法轻松完成此操作:
或将
NSOperation
与NSOperationQueue
结合使用。请查看此问题了解更多详细信息。
注意:
Why don't you use threads?
You can do it easily by using:
or by using
NSOperation
with aNSOperationQueue
.Have a look at this question for more details.
Notes:
在这里添加太多并发可能是错误的方法。您可能想要研究设置计时器并在主线程上播放音频文件,同时在后台线程上执行阻塞文件写入调用。这里的重要概念是远离主线程上的阻塞调用,尤其是在处理文件或网络 I/O 时。
您需要避免创建太多额外的线程,因为 iPhone 是单 CPU 设备,多个线程会导致更多的资源消耗。
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.