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

查看:116
本文介绍了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天全站免登陆