异步方法同步执行
1.dispatch_group
- (NSInteger)methodSyncDispatchGroup
{
__block NSInteger result = 0;
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[self methodAsync:^(NSInteger value) {
result = value;
dispatch_group_leave(group);
}];
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
return result;
}
2.dispatch_semaphore
- (NSInteger)methodSyncSemaphore
{
__block NSInteger result = 0;
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[self methodAsync:^(NSInteger value) {
result = value;
dispatch_semaphore_signal(sema);
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
return result;
}