如何解决等待完成块,并且控制到达非空等待异步结束
我想用两层异步完成块来实现BOOL函数
__strong auto weakSelf = self;
[_functionA complete:^{
__strong auto strongSelf = weakSelf;
if (strongSelf) {
[strongSelf functionB:param1 complete:^{
return self->_property1;
}];
}}];
它将抱怨“控制到达非void功能的末尾”;但是我无法添加默认返回值,否则它将首先返回该值,而无需等待functionB完成。当这里有两个完成区块的包裹时,就会变得更加困难。
任何建议将不胜感激!
解决方法
含义
control reaches end of non-void function
是的:
编译器无法从该代码中判断该函数是否会 到达终点,仍然返回一些东西。
您有一个非空方法,在任何return
条件下都缺少if-else
。
通过查看您的代码好像您使用block返回了BOOL
。这是不可能的,因为您的功能和块在不同的范围内执行。
检查是否有帮助:
__strong auto weakSelf = self;
return [_functionA complete:^{
__strong auto strongSelf = weakSelf;
if (strongSelf) {
return [strongSelf functionB:param1 complete:^{
return self->_property1;
}];
} else {
return YES;
}
}];
,
您可能需要使用semaphore
等到异步代码完成。
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
BOOL result = false; // default value
__strong auto weakSelf = self;
[_functionA complete:^{
__strong auto strongSelf = weakSelf;
if (strongSelf) {
[strongSelf functionB:param1 complete:^{
result = self->_property1;
dispatch_semaphore_signal(semaphore);
}];
} else {
dispatch_semaphore_signal(semaphore);
}
}];
dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER);
dispatch_release(semaphore);
return result;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。