проблема работает и останавливается CFRunLoop

У меня есть ситуация, когда мне нужно подождать, пока один блок будет завершен, а затем только двигаться вперед с моим кодом, для этого я использую CFRunLooprun и остановлюсь, вот как это сделать, я объясню больше вещей в комментариях в моем коде

 [self fatchAllEvent];  // BLOCK IS IN THIS METHOD

NSLog(@"loop will start");
CFRunLoopRun();NSLog(@"LOOP IS STOOPED");

-(void)fatchAllEvent{events = [[NSMutableArray alloc]init];//    // Get the appropriate calendar
NSCalendar *calendar = [NSCalendar currentCalendar];eventStore = [[EKEventStore alloc] init];

if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
//        __block typeof (self) weakSelf = self; // replace __block with __weak if you are using ARC
dispatch_async(dispatch_get_main_queue(), ^{
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{

if (granted)
{

[events removeAllObjects];
NSLog(@" granted");
NSLog(@"User has granted permission!");
// Create the start date components
NSDateComponents *twoYearAgoComponents = [[NSDateComponents alloc] init];
twoYearAgoComponents.year = -2;
NSDate *oneDayAgo = [calendar dateByAddingComponents:twoYearAgoComponents
toDate:[NSDate date]
options:0];

// Create the end date components
NSDateComponents *twoYearFromNowComponents = [[NSDateComponents alloc] init];
twoYearFromNowComponents.year = 2;
NSDate *oneYearFromNow = [calendar dateByAddingComponents:twoYearFromNowComponents
toDate:[NSDate date]
options:0];

// Create the predicate from the event store's instance method
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:oneDayAgo
endDate:oneYearFromNow
calendars:nil];

// Fetch all events that match the predicate
events =(NSMutableArray*) [eventStore eventsMatchingPredicate:predicate];
NSLog(@"The content of array is%@",events);}
else
{
NSLog(@"Not granted");}
NSLog(@"LOOP WILL STOP");  // THIS GETS PRINT
CFRunLoopStop(CFRunLoopGetCurrent());   // BUT LOOP IS NOT STOPPING HERE SO MY APP JUST GET HANGED ;
}];

});
}
else
{

[events removeAllObjects];

NSLog(@"Autometiclly granted permission!");
// Create the start date components
NSDateComponents *twoYearAgoComponents = [[NSDateComponents alloc] init];
twoYearAgoComponents.year = -2;
NSDate *oneDayAgo = [calendar dateByAddingComponents:twoYearAgoComponents
toDate:[NSDate date]
options:0];

// Create the end date components
NSDateComponents *twoYearFromNowComponents = [[NSDateComponents alloc] init];
twoYearFromNowComponents.year = 2;
NSDate *oneYearFromNow = [calendar dateByAddingComponents:twoYearFromNowComponents
toDate:[NSDate date]
options:0];

// Create the predicate from the event store's instance method
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:oneDayAgo
endDate:oneYearFromNow
calendars:nil];

// Fetch all events that match the predicate
events =(NSMutableArray*) [eventStore eventsMatchingPredicate:predicate];
NSLog(@"The content of array is%@",events);}}

0

Решение

Вы не можете сделать это таким образом — вы должны вызвать свою функцию, а затем в конце блока асинхронной диспетчеризации, в конце, вызвать функцию, продолжающую то, что вы хотите сделать.

В приведенном выше коде вы смешиваете асинхронное программирование с синхронным выполнением, которое не будет работать.

0

Другие решения

Других решений пока нет …