EKEventStore blocks ui thread

277 views Asked by At

I am trying to ask the user for permissions to edit the calendars

I put logs to display the time it takes for each method to run, and it says that it was for few milliseconds

the problem is, right after i request the the permission, the ui thread seems to be blocked for over a minute

the logs say this :

2014-11-09 22:09:07.236 FixGift[60052:22b] I,-[EACalendarManager getLocalEventCalendarsWithHandler:]:91
[fg16,179,176;before start request[;
2014-11-09 22:09:08.795 App[60052:4607] I,__55-[EACalendarManager getLocalEventCalendarsWithHandler:]_block_invoke:101
[fg16,179,176;before start callback[;
2014-11-09 22:09:08.805 App[60052:4607] I,__42-[EANewEventViewController askPermission:]_block_invoke:433
[fg16,179,176;before save event[;
2014-11-09 22:09:08.807 App[60052:4607] I,__69-[EANewEventViewController saveEventToCalendarWithManager:eventInfo:]_block_invoke:443
[fg16,179,176;created calendar[;
2014-11-09 22:09:08.840 App[60052:60b] I,-[EABaseViewController dealloc]:106
[fg16,179,176;deallocated:EABaseViewController[;
2014-11-09 22:09:08.841 App[60052:60b] I,-[EABaseViewController dealloc]:106
[fg16,179,176;deallocated:EABaseViewController[;
2014-11-09 22:09:08.840 App[60052:4607] I,__42-[EANewEventViewController askPermission:]_block_invoke:435
[fg16,179,176;after save event[;
2014-11-09 22:09:08.842 App[60052:4607] I,__55-[EACalendarManager getLocalEventCalendarsWithHandler:]_block_invoke:107
[fg16,179,176;after callback[;

-(void)getLocalEventCalendarsWithHandler:(EACalendarManagerEventsCallback)callback{
    __weak typeof (self)weakself = self;
    if(!self.eventsAccessGranted){
        LogInfo(@"before start request");
        [self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {

            if(!granted){
                NSLog(@"Error requesting access to events:%@",error);
                if(callback){
                    callback(granted,error,nil);
                }
                return ;
            }
            LogInfo(@"before start callback");
            NSArray *calendars =[weakself getLocalEventCalendars];

            if(callback){
                callback(granted,error,calendars);
            }
            LogInfo(@"after callback");
        }];
    }else {
        NSArray *calendars =[self getLocalEventCalendars];
        if(callback){
            callback(YES,nil,calendars);
        }
    }
}

what can i do to fix the problem ?

1

There are 1 answers

0
Paulw11 On BEST ANSWER

Your completion handler will be executing on a background thread. All updates to the UI should be performed on the main queue.

You can either dispatch on the main queue inside your callback block or it may be safer to invoke the callback on the main queue.