title: IOS/Objective-C: Retrieve NSArray from completion block
body: <p>I am trying to retrieve an array of <code>EKReminders</code> from a completion block of a method.  The method returns the array I desire.  However, I can't find a good way to obtain it back on the main thread at known time such that that I know the block has completed.</p>
  
  <p>To retrieve <code>EKReminders</code>, Apple requires a completion block approach (in contrast to <code>EKEvents</code>.)  So my fetch code contains the following:</p>
  
  <pre><code>-(void) fetchReminders{
  [self.eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders) {
          self.reminders = reminders;
   }];
  }
  </code></pre>
  
  <p>The above code successfully runs and places reminders in the ivar once finished..</p>
  
  <p>The problem is I don't know when the block completes so if I merely try to access self.reminders in viewwillappear after callng the above method, it returns empty presumably because the block has not yet completed  I would like some way to set the ivar when I know that the block has completed to eliminate the race condition.</p>
  
  <p>A number of answers on SO for how to retrieve a value form a completion block recommend passing in a completion block to the method as follows:</p>
  
  <pre><code>//change above method to 
  -(void) fetchReminders withCompletion:(void (^)(NSArray *reminders))completion; {
  
  and call it in viewwillappear with:
    [self fetchReminders withCompletion:^(NSArray* reminders) {
          self.reminders =remindersArray;
          NSLog(@"remindersArray in viewwillappear%@",remindersArray);
      }];
  </code></pre>
  
  <p>When I try this, the completion block in <code>viewwillappear</code> never fires so something is going wrong.</p>
  
  <p>Would appreciate any suggestions on how to successfully retrieve at a known point in time an array retrieved within a completion block.</p>