NSOperation 等待事件 [英] NSOperation wait for event
本文介绍了NSOperation 等待事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个扩展 NSOperation 的自定义类,以便通过蓝牙与另一台设备成功通信.我的问题是如何实现类的 main 方法,使其等待蓝牙触发的事件?
I'd like to make a custom class that extends NSOperation in order to make successful communication with another device by bluetooth. The question I have is how can I implement the main method of the class so that it will wait for an event triggered by the bluetooth?
推荐答案
永远不要使用无限循环.它不节能.正如@lead_the_zeppelin 所说,使用信号量/互斥量/等.例如,您可以像这样使用 dispatch_group
函数:
Never use an infinite loop. It is not energy-efficient. As @lead_the_zeppelin said, use semaphore/mutex/etc. For example you may use dispatch_group
functions like this:
dispatch_group_t waitGroup = dispatch_group_create();
dispatch_group_enter(waitGroup);
dispatch_async(otherQueue, ^{
//long-running code
dispatch_group_leave(waitGroup);
}
dispatch_group_wait(waitGroup, DISPATCH_TIME_FOREVER);
这篇关于NSOperation 等待事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文