title: Parse.com query for 10000 (10K) objects
body: <p>I have a parse database with a class named <code>MeetingObject</code> filled with 6000 objects (it will grow by the way...).</p>
  
  <p>Being the parse query limit 1000 I'm trying to get them using the skip query property.</p>
  
  <p>The following code gives me 2000 objects:</p>
  
  <pre><code>NSMutableArray *allObjects = [NSMutableArray array];
  NSUInteger limit = 1000;
  __block NSUInteger skip = 0;
  [query setLimit: limit];
  [query setSkip: skip];
  [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
      if (!error) {
          [allObjects addObjectsFromArray:objects];
          NSLog(@"%lu", (unsigned long)allObjects.count );
  
          if (objects.count == limit) {
              // There might be more objects in the table. Update the skip value and execute the query again.
              skip += limit;
              [query setSkip: skip];
              [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                  [allObjects addObjectsFromArray:objects];
                  NSLog(@"%lu", (unsigned long)allObjects.count );
              }];
          }
  
      }  else if (error || [error code] == kPFErrorConnectionFailed) {
  
          UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!" message:NSLocalizedString(@"The Internet connection appears to be offline.",@"no internet") delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
          self.navigationItem.rightBarButtonItem.enabled = YES;
          self.tableView.userInteractionEnabled = YES;
          [alertView show];
          return;
      }
  }];
  </code></pre>
  
  <p>It is my understanding that if I want to get 1000 more objects I have to add another nested query, then another for the next 1000 and so on like this:</p>
  
  <pre><code>// finding the first 1000 objects
  [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
      if (!error) {
          [allObjects addObjectsFromArray:objects];
          NSLog(@"%lu", (unsigned long)allObjects.count );
  
          if (objects.count == limit) {
              // finding another 1000 objects
              skip += limit;
              [query setSkip: skip];
              [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                  [allObjects addObjectsFromArray:objects];
                  NSLog(@"%lu", (unsigned long)allObjects.count );
  
                  if (objects.count == limit) {
                      // finding another 1000 objects
                      skip += limit;
                      [query setSkip: skip];
                      [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                          [allObjects addObjectsFromArray:objects];
                          NSLog(@"%lu", (unsigned long)allObjects.count );
                      }];
                  }
  
              }];
  
          }
  </code></pre>
  
  <p>but what if I don't know the exact number of objects? I tried using:</p>
  
  <pre><code>while (objects.count == limit) {
          // There might be more objects in the table. Update the skip value and execute the query again.
          skip += limit;
          [query setSkip: skip];
          [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
              [allObjects addObjectsFromArray:objects];
              NSLog(@"%lu", (unsigned long)allObjects.count );
          }];
      }
  </code></pre>
  
  <p>but I get</p>
  
  <blockquote>
    <p><em>*</em> Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This query has an outstanding network connection. You have to wait until it's done.'</p>
  </blockquote>
  
  <p>because of course the queries are made before the last one is finished...</p>