0

i have situation where i need to wait till one block is get completed and then only move forward with my code for that i use CFRunLooprun and stop this is how do it i will explain more things in comment in my code

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




    }


}
Prakash Desai
  • 511
  • 3
  • 14

1 Answers1

0

You can't do that this way - you have to call your function, and then in the completion block of the async dispatch, at the end, call a function that continues with what you want to do.

In your above code you mix up asynchronous programming with synchronous execution, that won't work.

TheEye
  • 9,280
  • 2
  • 42
  • 58
  • if i do then that function also goes in that waiting phase and my current method keep on execution and returns null then which i do not want – Prakash Desai Aug 16 '13 at 09:36
  • It may be that that is what you don't want, but that's the way it works with asynchronous programming. The only other way is blocking the execution thread (probably the main thread in your case), and that is really - and I mean REALLY - bad design and a horrible user experience. – TheEye Aug 16 '13 at 11:41