Parse.com 查询 10000 (10K) 个对象 [英] Parse.com query for 10000 (10K) objects

查看:22
本文介绍了Parse.com 查询 10000 (10K) 个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个解析数据库,其中包含一个名为 MeetingObject 的类,其中包含 6000 个对象(顺便说一下,它会增长......).

I have a parse database with a class named MeetingObject filled with 6000 objects (it will grow by the way...).

作为解析查询限制 1000,我正在尝试使用跳过查询属性来获取它们.

Being the parse query limit 1000 I'm trying to get them using the skip query property.

以下代码给了我 2000 个对象:

The following code gives me 2000 objects:

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 个以上的对象,我必须添加另一个嵌套查询,然后为接下来的 1000 个添加另一个嵌套查询,依此类推:

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:

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

            }];

        }

但是如果我不知道对象的确切数量怎么办?我尝试使用:

but what if I don't know the exact number of objects? I tried using:

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"而终止应用程序,原因:此查询具有未完成的网络连接.你必须等到它完成.'

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This query has an outstanding network connection. You have to wait until it's done.'

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

because of course the queries are made before the last one is finished...

推荐答案

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

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

这篇关于Parse.com 查询 10000 (10K) 个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆