2016-01-18 114 views
0

在我的應用程序中,我使用異步類方法,我需要同步運行。信號量同步運行異步方法

據我瞭解,我應該使用信號來完成這一點。使用信號量是我以前從未做過的事情,所以現在我正在努力把它們放在正確的位置。

我希望你們中的一些人能幫我一把。

這是我的代碼:

-(void)CreateNewerEventInCalendar:(Event*)myEvent{ 
    [MyCalendar requestAccess:^(BOOL granted, NSError *error) { 
       if (granted) { 
      BOOL result = [MyCalendar addEventAt:myEvent.StartPoint withTitle:myEvent.Subject inLocation:myEvent.Location]; 
      if (result) { 
       // added to calendar 
      } else { 
       // unable to create event/calendar 
      } 
     } else { 
      // you don't have permissions to access calendars 
     } 
    }]; 

} 
+0

此外,一個很好的資源,瞭解如何信號燈工作原理是:http://greenteapress.com/semaphores/ –

回答

0

試試這個:

dispatch_semaphore_t sema = dispatch_semaphore_create(0); 

[MyCalendar requestAccess:^(BOOL granted, NSError *error) { 

    // your implementation 
    dispatch_semaphore_signal(sema); 
}]; 

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); 
// continue here