我正在尝试在objective C
中实现并发。我对需要以同步方式运行的操作有疑问。这里的问题是我使用的函数在完成后执行一个 block 。
我想连接到蓝牙设备以运行一些操作并连接到下一个设备。
for (Beacon * beacon in beacons) {
[beacon setDelegate:self];
[beacon connectToBeacon];
}
但是连接是异步的。当连接成功时,信标调用委托(delegate)(在本例中是同一个类)方法 didConnectSuccess
。
在连接到下一个设备之前,我需要等待“beaconDidConnect
”中的所有操作和断开连接完成。
我目前使用调度队列和调度信号量的组合,我的信号量是一个ivar
dispatch_queue_t myCustomQueue;
myCustomQueue = dispatch_queue_create("com.example.MyCustomQueue", NULL);
for (Beacon * beacon in beacons) {
[beacon setDelegate:self];
dispatch_async(myCustomQueue, ^{
dispatch_semaphore_wait(semaphoreBluetooth, DISPATCH_TIME_FOREVER);
[beacon connectToBeacon];
});
}
结合
- (void)beaconDidDisconnect:(Beacon *)beacon
{
dispatch_semaphore_signal(semaphoreBluetooth);
}
如果没有 dispatch_async,通过阻塞回调 (beaconDidConnect),等待会导致死锁。
我想在 for 循环中 dispatch_semaphore_wait
而不是在调度 block 中,但是等待导致回调再次等待,从而导致死锁。
这种方式似乎可行,但我发现它有点难看。
我的另一个问题是,在我的 beaconDidConnect
方法中,我需要链接 asynchronous
调用并在每个等待前一个终止的过程中。
所有这些调用都有一个终止 block ,在调用完成时执行。我可以在越来越深的 block 中编写指令,但我想避免这种情况。
我需要一个等效于 javascript“ promise ”的概念。
目前我有一些关于调度队列和调度信号量的东西,但我有时会因为未知原因而死锁。
例如:
- (void)beaconConnectionDidSucceeded:(Beacon *)beacon
{
dispatch_semaphore_t semaphoreEditing = dispatch_semaphore_create(1);
dispatch_queue_t editingQueue = dispatch_queue_create("com.example.MyCustomQueue.Editing", NULL);
// First writing procedure
dispatch_async(editingQueue, ^{
dispatch_semaphore_wait(semaphoreEditing, DISPATCH_TIME_FOREVER);
[beacon writeSomeCaracteristic:caracteristic withValue:value withCompletion:^(void) {
dispatch_semaphore_signal(semaphoreEditing);
}];
});
// A unknow number of writing sequences
dispatch_async(editingQueue, ^{
dispatch_semaphore_wait(semaphoreEditing, DISPATCH_TIME_FOREVER);
[beacon writeSomeCaracteristic:caracteristic withValue:value withCompletion:^(void) {
dispatch_semaphore_signal(semaphoreEditing);
}];
});
//
// ...
//
dispatch_async(editingQueue, ^{
dispatch_semaphore_wait(semaphoreEditing, DISPATCH_TIME_FOREVER);
[beacon writeSomeCaracteristic:caracteristic withValue:value withCompletion:^(void) {
dispatch_semaphore_signal(semaphoreEditing);
}];
});
// Terminate the edition
dispatch_async(editingQueue, ^{
dispatch_semaphore_wait(semaphoreEditing, DISPATCH_TIME_FOREVER);
[beacon disconnectBeacon];
dispatch_semaphore_signal(semaphoreEditing);
});
}
我想编写清晰的代码,按顺序执行我的指令。
最佳答案
如果您的异步方法确实有一个完成处理程序,您可以“序列化”或“链接”多个异步调用,如下所示:
[self asyncFooWithCompletion:^(id result){
if (result) {
[self asyncBarWithCompletion:^(id result){
if (result) {
[self asyncFoobarWithCompletion:^(id result){
if (result) {
...
}
}];
}
}];
}
}];
当然,这会随着链式异步调用的数量而变得越来越困惑,尤其是当您也想处理错误时。
借助专门帮助克服这些问题(包括错误处理、取消)的第三方库,它可能类似于以下代码:
给定:
- (Promise*) asyncFoo;
- (Promise*) asyncBar;
- (Promise*) asyncFoobar;
“链接”三个异步方法,包括错误处理:
[self asyncFoo]
.then(^id(id result){
... // do something with result of asyncFoo
return [self asyncBar];
}, nil)
.then(^id (id result){
... // do something with result of asyncBar
return [self asyncFoobar];
}, nil)
.then(^id(id result) {
... // do something with result of asyncFoobar
return nil;
},
^id(NSError*error){
// "catch" any error from any async method above
NSLog(@"Error: %@", error);
return nil;
});
有关“Promises”的一般信息,请阅读维基文章 Futures and Promises .
有许多实现 Promise 的 Objective-C 库。
关于ios - 处理并发和异步响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20996673/