Parse.com query for 10000 (10K) objects

馋奶兔 提交于 2019-12-04 19:45:32

what I would do is to fetch the number of objects first, then query using skip method.

PFQuery *query = [PFQuery queryWithClassName:@"GameScore"];
[query whereKey:@"playername" equalTo:@"Sean Plott"];
[query countObjectsInBackgroundWithBlock:^(int count, NSError *error) {
  if (!error) {
     dispatchFindObjectsUsingLimit(count)
  } else {
    // The request failed
  }
}];

although this is a late response, this could help other people who come across this problem. Basically, just like Simon's answer above, step 1 is get a count and then create a group dispatch to loop through each request until all data has been downloaded. This is my (slightly generic) code I used:

- (void)getLargeDataFromParseWithBlock:(void (^)(NSArray *, NSError *))block {
  int max = 1000;

  __block NSError *error;
  PFQuery *query = [PFQuery queryWithClassName:@"<your class>"];
  [query whereKey:@"<field name in class>" equalTo:@"xyz"];
  query.limit = max;
  // get count of ojects first
  [query countObjectsInBackgroundWithBlock:^(int count, NSError *_error) {
    if (!error) {
      // calculate how many requests are need based on count and max
      int APIrequests = (count+max-1)/max;
      // create a group dispatch
      dispatch_group_t downloadGroup = dispatch_group_create();
      for (int i=0; i<APIrequests; i++) {
        // tell dispatch a task is starting
        dispatch_group_enter(downloadGroup);
        PFQuery *dispatchQuery = [PFQuery queryWithClassName:@"<your class>"];
        [dispatchQuery whereKey:@"<field name in class>" equalTo:@"xyz"];
        dispatchQuery.limit = max;
        dispatchQuery.skip = i*max;
        [dispatchQuery findObjectsInBackgroundWithBlock:^(NSArray *arrayResponse, NSError *_error2) {
          if (!_error2) {
            NSLog(@"Successfully retrieved %lu.", (unsigned long)arrayResponse.count);

            // do something with arrayResponse like add to core data or sqlite

            // tell dispatch task has completed
            dispatch_group_leave(downloadGroup);
          } else {
            NSLog(@"Error: %@ %@", _error2, [_error2 userInfo]);
            error = _error2;
            // tell dispatch task has completed - need to cover suuccess and fail scenarios of parse request
            dispatch_group_leave(downloadGroup);
          }
        }];
      }
      // called when no more tasks in dispatch
      dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{
        if (block) {
          if (!error) {
            // array could contain combined responses - sourced from core data or sqlite for example
            block(@[], nil);
          } else {
            block(nil, error);
          }
        }
      });
    }else {
      block(nil, _error);
      NSLog(@"Count error: %@ %@", _error, [_error userInfo]);
    }
  }];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!