iOS异步变同步

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
__block BOOL result = NO;
//异步线程中操作是否完成
__block BOOL inThreadOperationComplete = NO;
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if (settings.authorizationStatus == UNAuthorizationStatusDenied) {
result = NO;
}else if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) {
result = NO;
}else if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
result = YES;
}else {
result = NO;
}
inThreadOperationComplete = YES;
}];

while (!inThreadOperationComplete) {
[NSThread sleepForTimeInterval:0];
}
return result;

另外一种写法:使用信号量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- (void)testDispatchSemaphore {
NSLog(@"--------------------------begin-----------------");
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_queue_t que = dispatch_get_global_queue(0, 0);

dispatch_async(que, ^{
NSLog(@"--------------------------async begin-----------------");
[NSThread sleepForTimeInterval:3];
dispatch_semaphore_signal(sema);
NSLog(@"--------------------------async complete-----------------");
});


dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
NSLog(@"--------------------------complete-----------------");

}

--------------------------begin-----------------
--------------------------async begin-----------------
--------------------------async complete-----------------
--------------------------complete-----------------