简体   繁体   中英

Indicating to main thread that Async call for calendar access has been completed

I want calendar access to the user's iphone for that i am using the async call

__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"]
    }

}];

I basically want to do some processing and then signal the main thread which is executing in another object that the processing has been done on someData and that it should now access it.

I have attempted wrapping this in a dispatch group like this

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);
});

however since the call is async it still notifies the group of completion before any actual completion has beeen done.

Basically how can i just signal the main thread that processing has been done on the async call, get the results and start doing your own processing

Add a notification on viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(calendarPermissionGranted:) name:@"calendarProcessingCompleted" object:nil];

once the calendar permission granted it will post the notification from here.

 __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];
              });
        }
}];

Capture it in method

-(void)calendarPermissionGranted:(NSNotification *)noti{

}

May be this what you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM