Ios7 Parse.com查询10000(10K)个对象

Ios7 Parse.com查询10000(10K)个对象,ios7,xcode5,parse-platform,Ios7,Xcode5,Parse Platform,我有一个parse数据库,其中一个名为MeetingObject的类填充了6000个对象(顺便说一下,它会增长…) 作为解析查询限制1000,我尝试使用skip query属性获取它们 以下代码为我提供了2000个对象: NSMutableArray *allObjects = [NSMutableArray array]; NSUInteger limit = 1000; __block NSUInteger skip = 0; [query setLimit: limit]; [query

我有一个parse数据库,其中一个名为
MeetingObject
的类填充了6000个对象(顺便说一下,它会增长…)

作为解析查询限制1000,我尝试使用skip query属性获取它们

以下代码为我提供了2000个对象:

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;
    }
}];
我的理解是,如果我想获得1000多个对象,我必须添加另一个嵌套查询,然后再添加另一个嵌套查询,以此类推:

// 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 );
                    }];
                }

            }];

        }
但是如果我不知道物体的确切数量呢?我尝试使用:

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 );
        }];
    }
但我明白了

*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“此查询具有未完成的网络连接。你必须等到它完成。”


因为查询当然是在最后一个完成之前进行的…

我要做的是首先获取对象的数量,然后使用skip方法进行查询

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
  }
}];

虽然这是一个迟来的反应,但这可以帮助其他遇到这个问题的人。 基本上,就像上面Simon的回答一样,第1步是获得一个计数,然后创建一个组调度,循环处理每个请求,直到所有数据都下载完毕。这是我使用的(稍微通用的)代码:

- (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]);
    }
  }];
}
-(void)getLargeDataFromParseWithBlock:(void(^)(NSArray*,NSError*)块{
int max=1000;
__块N错误*错误;
PFQuery*query=[PFQuery queryWithClassName:@”“;
[查询whereKey:@“equalTo:@“xyz”];
query.limit=max;
//首先获取项目数
[query countObjectsInBackgroundWithBlock:^(整数计数,N错误*\U错误){
如果(!错误){
//根据计数和最大值计算需要多少请求
int API请求=(计数+max-1)/max;
//创建组调度
dispatch_group_t downloadGroup=dispatch_group_create();

对于(int i=0;此链接回答您的问题: