c++ - issue is running and stopping CFRunLoop -


i have situation need wait till 1 block completed , move forward code use cfrunlooprun , stop how explain more things in comment in code

 [self fatchallevent];  // block in method       nslog(@"loop start");     cfrunlooprun();       nslog(@"loop stooped");  -(void)fatchallevent{        events = [[nsmutablearray alloc]init];   //    // appropriate calendar     nscalendar *calendar = [nscalendar currentcalendar];       eventstore = [[ekeventstore alloc] init];      if ([eventstore respondstoselector:@selector(requestaccesstoentitytype:completion:)])     { //        __block typeof (self) weakself = self; // replace __block __weak if 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 start date components                  nsdatecomponents *twoyearagocomponents = [[nsdatecomponents alloc] init];                  twoyearagocomponents.year = -2;                  nsdate *onedayago = [calendar datebyaddingcomponents:twoyearagocomponents                                                                todate:[nsdate date]                                                               options:0];                   // create end date components                  nsdatecomponents *twoyearfromnowcomponents = [[nsdatecomponents alloc] init];                  twoyearfromnowcomponents.year = 2;                  nsdate *oneyearfromnow = [calendar datebyaddingcomponents:twoyearfromnowcomponents                                                                     todate:[nsdate date]                                                                    options:0];                   // create predicate event store's instance method                  nspredicate *predicate = [eventstore predicateforeventswithstartdate:onedayago                                                                               enddate:oneyearfromnow                                                                             calendars:nil];                   // fetch events match predicate                  events =(nsmutablearray*) [eventstore eventsmatchingpredicate:predicate];                  nslog(@"the content of array is%@",events);                 }              else              {                  nslog(@"not granted");                 }                 nslog(@"loop stop");  // gets print                  cfrunloopstop(cfrunloopgetcurrent());   // loop not stopping here app hanged ;            }];                 });     }     else     {           [events removeallobjects];          nslog(@"autometiclly granted permission!");         // create start date components         nsdatecomponents *twoyearagocomponents = [[nsdatecomponents alloc] init];         twoyearagocomponents.year = -2;         nsdate *onedayago = [calendar datebyaddingcomponents:twoyearagocomponents                                                       todate:[nsdate date]                                                      options:0];          // create end date components         nsdatecomponents *twoyearfromnowcomponents = [[nsdatecomponents alloc] init];         twoyearfromnowcomponents.year = 2;         nsdate *oneyearfromnow = [calendar datebyaddingcomponents:twoyearfromnowcomponents                                                            todate:[nsdate date]                                                           options:0];          // create predicate event store's instance method         nspredicate *predicate = [eventstore predicateforeventswithstartdate:onedayago                                                                      enddate:oneyearfromnow                                                                    calendars:nil];          // fetch events match predicate         events =(nsmutablearray*) [eventstore eventsmatchingpredicate:predicate];         nslog(@"the content of array is%@",events);         }   } 

you can't way - have call function, , in completion block of async dispatch, @ end, call function continues want do.

in above code mix asynchronous programming synchronous execution, won't work.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -