title: dispatch_group_t issue, dispatch_group_notify is calling back before leaving the group
body: <p>I have the following snippet of code below that fetches data from Parse using PFQueues in the background and returns data and a status. This structure is based off of waiting for the dispatch_group_t to notify that's it's completed all entered groups. Unfortunately <code>dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{</code>
  is called before the completion blocks call dispatch_group_leave. By the time the dispatch_group_leave() is called on any of the completion blocks, an EXC_BAD_INSTRUCTION is thrown. I've attached an image below for the instruction error. Does anyone know if I'm doing something wrong or if Parse has some annoyances that prevent me from using this method?</p>
  
  <pre><code>    - (void)downloadAndCacheObjectsWithCompletion:(void (^)(NSError *))callback
  {
  
      __block NSError *downloadError1;
      __block NSError *downloadError2;
      __block NSError *downloadError3;
      __block NSError *downloadError4;
  
      NSLog(@"%@", NSStringFromSelector(_cmd));
  
          dispatch_group_t downloadGroup = dispatch_group_create();
  
          dispatch_group_enter(downloadGroup);
          [self fetchDataWithCompletion:^(NSArray *artwork, NSError *error) {
              downloadError1 = error;
              dispatch_group_leave(downloadGroup);
          }];
  
          dispatch_group_enter(downloadGroup);
          [self fetchDataWithCompletion:^(NSArray *artworkPhotos, NSError *error) {
              downloadError2 = error;
              dispatch_group_leave(downloadGroup);
          }];
  
          dispatch_group_enter(downloadGroup);
          [self fetchDataWithCompletion:^(NSArray *artists, NSError *error) {
              downloadError3 = error;
              dispatch_group_leave(downloadGroup);
          }];
  
          dispatch_group_enter(downloadGroup);
          [self fetchDataWithCompletion:^(NSArray *badges, NSError *error) {
              downloadError4 = error;
              dispatch_group_leave(downloadGroup);
          }];
  
  
          dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{
              NSError *returnError;
              if (downloadError1 || downloadError2 || downloadError3 || downloadError4) {
                  returnError = [[NSError alloc] initWithDomain:@"ParseFactory" code:-1 userInfo:@{NSLocalizedDescriptionKey: @"There was an error retrieving the content"}];
              }
              if (callback) {
                callback(returnError);
              }
          });
  
  }
  
  
  
   - (void)fetchDataWithCompletion:(void(^)(NSArray *data, NSError *error))callback
  {
      NSLog(@"Fetching Data");
      if ([self.cachedData objectForKey:kDataClassName]) {
          if (callback) {
              callback([self.cachedData objectForKey:kDataClassName], nil);
          }
          return;
      }
      PFQuery *dataQueue = [PFQuery queryWithClassName:kDataClassName];
      dataQueue.cachePolicy = kPFCachePolicyCacheThenNetwork;
      [dataQueue findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
  
          if (!error) {
              [self.cachedData setObject:objects forKey:kDataClassName];
          } else {
             NSLog(@"Fetching Data Error: %@", error);
          }
          if (callback) {
              callback(objects, error);
          }
      }];
  
  }
  </code></pre>
  
  <p>The download process listed above is called from AppDelegate as such</p>
  
  <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  {
  //Register PFObject subclasses
      [Data registerSubclass];
  
  [Parse setApplicationId:@"appkey" clientKey:@"clientkey"];
      [[ParseFactory sharedInstance] downloadAndCacheObjectsWithCompletion:^(NSError *error) {
  
      }];
  
      return YES;
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/8oXep.png" alt="enter image description here"></p>
  
  <p>Stack trace:
  <img src="https://i.stack.imgur.com/R8xYj.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/Kcnq2.png" alt="enter image description here"></p>