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

时间:2014-07-30 09:53:37

标签: ios7 xcode5 parse-platform

我有一个解析数据库,其中一个名为MeetingObject的类填充了6000个对象(顺便说一句,它会增长...)。

作为解析查询限制1000我试图使用跳过查询属性来获取它们。

以下代码为我提供了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',原因:'此查询具有出色的网络连接。你必须等到它完成。'

因为查询是在最后一个查询完成之前完成的......

2 个答案:

答案 0 :(得分:2)

虽然这是一个迟到的回复,但这可以帮助遇到此问题的其他人。 基本上,就像上面的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]);
    }
  }];
}

答案 1 :(得分:0)

我要做的是先获取对象数,然后使用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
  }
}];