I'm building a IoT device and I have asynchronous reads working great, but I need to perform a synchronous read. I've been told this is possible on Android as there is a method that basically reads the buffer as it comes in.
我正在构建一个IoT设备,我的异步读取工作很好,但我需要执行同步读取。我被告知这在Android上是可行的,因为有一种方法基本上可以读取缓冲区。
I'm wondering if Core-bluetooth supports something similar or if anyone has a clever approach to this?
我想知道Core-bluetooth是否支持类似的东西,或者是否有人对此有一个聪明的方法?
1 个解决方案
#1
1
Any asynchronous operation can be made synchronous by blocking until the operation completes. You can use a queue or a lock to block.
通过阻塞可以使任何异步操作同步,直到操作完成。您可以使用队列或锁来阻止。
NSLock *lock = [[NSLock alloc] init];
[thing doSomethingAsynchronousWithCompletion:^{
[lock unlock];
}];
[lock lock];
Do not do this. Not ever.
不要这样做。永远不会。
You shouldn't block. Your asynchronous code's completion handler should trigger an event that causes your code to continue.
你不应该阻止。异步代码的完成处理程序应该触发导致代码继续的事件。
[thing doSomethingAsynchronousWithCompletion:^(NSData *readData){
[dataProcessor processData:readData];
}];
#1
1
Any asynchronous operation can be made synchronous by blocking until the operation completes. You can use a queue or a lock to block.
通过阻塞可以使任何异步操作同步,直到操作完成。您可以使用队列或锁来阻止。
NSLock *lock = [[NSLock alloc] init];
[thing doSomethingAsynchronousWithCompletion:^{
[lock unlock];
}];
[lock lock];
Do not do this. Not ever.
不要这样做。永远不会。
You shouldn't block. Your asynchronous code's completion handler should trigger an event that causes your code to continue.
你不应该阻止。异步代码的完成处理程序应该触发导致代码继续的事件。
[thing doSomethingAsynchronousWithCompletion:^(NSData *readData){
[dataProcessor processData:readData];
}];