我正在尝试在objective C
中实现并发。我遇到了需要以同步方式运行的操作的问题。这里的问题是我使用完成后执行块的函数。
我想连接到蓝牙设备以运行某些操作并连接到下一个设备。
for (Beacon * beacon in beacons) {
[beacon setDelegate:self];
[beacon connectToBeacon];
}
但是连接是异步的。当连接成功时,信标会调用委托(在这种情况下,它是同一个类)方法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
而不是在调度块中,但等待导致回调再次等待,导致死锁。
这种方式似乎有效,但我发现它有点难看。
我的另一个问题是,在我的beaconDidConnect
方法中,我需要链接asynchronous
调用,并在每个等待前一个终止。
所有这些呼叫都有一个终止块,在呼叫完成时执行。我可以在越来越深的块中编写指令,但我想避免这种情况。
我需要等同于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);
});
}
我想编写清晰的代码,以顺序方式执行我的指令。
答案 0 :(得分:2)
如果你的异步方法确实有一个完成处理程序,你可以“序列化”或“链接”一些异步调用,如下所示:
[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”的一般信息,请阅读wiki文章Futures and Promises。
有许多Objective-C库实现了Promise。
答案 1 :(得分:0)
您是否考虑过使用NSOperation和NSOperationQueue?
如果您需要等待每个信标在继续之前运行一组操作,您可以将每组操作存储在NSOperation中,并将所有操作放在NSOperationQueue中,其maxConcurrentLimit为1.可能更容易取消/ pause / terminate每个操作,队列将处理并发。
答案 2 :(得分:0)