[英]Indicating to main thread that Async call for calendar access has been completed
我想要我正在使用異步調用的日歷訪問用戶的iPhone
__block NSMutableArray* someData;
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
//We are going to fetch events from today to the next 1 year
if (granted) {
//Processing
[someData addObject:@"Something Added"]
}
}];
我基本上想進行一些處理,然后向另一個對象中正在執行的主線程發出信號,表明該處理已在someData
完成,現在應該訪問它。
我試圖將其包裝在這樣的調度組中
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
calendarData = [calender returnCalenderEvents];
});
dispatch_group_notify(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
NSLog(@"%@",calendarData);
});
但是,由於該調用是異步的,它仍會在完成任何實際完成之前通知該完成組。
基本上,我該如何僅向主線程發出信號,表明異步調用已完成處理,獲取結果並開始進行自己的處理
在viewDidLoad上添加通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(calendarPermissionGranted:) name:@"calendarProcessingCompleted" object:nil];
授予日歷權限后,它將在此處發布通知。
__block NSMutableArray* someData;
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
//We are going to fetch events from today to the next 1 year
if (granted) {
//Processing
[someData addObject:@"Something Added"]
dispatch_async(dispatch_get_main_queue(), ^{
//notify your class that processing has been done.
[[NSNotificationCenter defaultCenter] postNotificationName:@"calendarProcessingCompleted" object:nil userInfo:nil];
});
}
}];
用方法捕獲
-(void)calendarPermissionGranted:(NSNotification *)noti{
}
可能就是您要找的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.