如何在iOS中使用PFQuery获取表中的所有数据?

时间:2015-02-25 06:24:16

标签: ios objective-c parse-platform pfquery

我是iOS和Parse Framework的新手我想从PFQuery中获取Parse表中的数据,如

NSUInteger limit = 1500;
PFQuery *query = [PFQuery queryWithClassName:@"MapInfo"];
[query setLimit: limit];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
    if (!error) {
        NSLog(@"Successfully retrieved: %@", objects);
    } else {
        NSString *errorString = [[error userInfo] objectForKey:@"error"];
        NSLog(@"Error: %@", errorString);
    }
}];

它正如我想要的那样工作,但它只给我1000个对象,我想在这里获取它包含的所有表格数据到2000对象,它会逐渐增加,请帮助我。

为此我现在写了一个像这样的代码,但它只是给我1000个对象

 NSMutableArray *allObjects = [NSMutableArray array];
NSUInteger limit = 0;
__block NSUInteger skip = 0;
PFQuery *query = [PFQuery queryWithClassName:@"MapInfo"];
[query setLimit: limit];
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        [allObjects addObjectsFromArray:objects];
        if (objects.count == limit) {

            skip += limit;
            [query setSkip: skip];
            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                NSLog(@"Array %@",objects);
            }];
             }

             } else {
                 NSLog(@"Error: %@ %@", error, [error userInfo]);
             }
             }];

感谢。

3 个答案:

答案 0 :(得分:2)

我认为可以获取的对象数量存在查询限制。我要做的是查询为同一件事做两个查询,但对于第二个查询,做这样的事情

[query setSkip1000];

你可以跳过第一个查询中的前1000个对象并获取下一个1000.使你的数组​​成为NSMutableArray并在每个块中执行此操作

[self.myArray addObjects:objects];而不是self.myArray = objects;,这样你就会覆盖数组中的对象。

修改

您可以执行此操作,而不是2个单独的查询

NSMutableArray *allObjectsArray = [NSMutableArray array];
//Set this to the amount of objects you want. Has to be be 1000 or less
NSUInteger limit = 0;

//Set this to the amount you want to skip (Usually 0)
NSUInteger skip = 0;
PFQuery *query = [PFQuery queryWithClassName:@"tempClass"];
[query setLimit: limit];
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
  if (!error) {
    // The find succeeded. Add the returned objects to allObjects
    [allObjectsArray addObjectsFromArray:objects];
    if (objects.count == limit) {
      // There could be more objects in your database. Update the skip number and perform the same query.
      skip = skip + limit;
      [query setSkip: skip];
      [query findObject...// Exactly the same way as you did before to get the rest of your objects in the database
    }

  } else {
    // Log details of the failure
    NSLog(@"Error: %@ %@", error, [error userInfo]);
  }
}];

答案 1 :(得分:2)

这是一个简单的递归解决方案,可以使用块从类中检索所有对象。

以下是您最初的称呼方式。

[self queryAllObjectswithLimit:1000 withSkip:0 withObjects:@[] withSuccess:^(NSArray * objects) {
    //All the objects
} failure:^(NSError * error) {
    //
}];

这是方法。

- (void)queryAllObjectswithLimit:(NSUInteger )limit withSkip:(NSUInteger )skip withObjects:(NSArray *)objects withSuccess:(void (^)(NSArray *objects))success failure:(void (^)(NSError *error))failure {
//Store all the Objects through each layer of recurrsion
NSMutableArray *allObjects = [NSMutableArray arrayWithArray:objects];
PFQuery *query = [PFQuery queryWithClassName:@"Class_Name"];
query.limit = limit;
query.skip = skip;
[query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
    if (!error) {
        // The find succeeded. Add the returned objects to allObjects
        [allObjects addObjectsFromArray:objects];
        if (objects.count == limit) {
            //Recursively Call this until count does not equal limit, then begin returning all the objects back up
            [self queryAllObjectswithLimit:limit withSkip:skip+limit withObjects:allObjects withSuccess:^(NSArray * objects) {
                //This will return everything
                success(objects);
            } failure:^(NSError * error) {
                failure(error);
            }];
        } else {
            success(allObjects);
        }
    } else {
        failure(error);
    }
}];

}

我用少于1000个对象,1000个对象和超过1000个对象测试了它,它完美地运行。

要小心你抓到了多少个对象,因为这会占用所有对象,如果你正在处理一个很大的数据集,这可能是内存方面的问题。

答案 2 :(得分:-2)

https://parse.com/questions/fetch-all-data-in-a-table-using-pfquery

您可以使用skip和limit参数对表中的所有对象进行分页,方法是添加要跳过的限制值,直到查询返回小于限制的对象数量。

NSMutableArray *allObjects = [NSMutableArray array];
NSUInteger limit = 0;
__block NSUInteger skip = 0;
PFQuery *query = [PFQuery queryWithClassName:@"MapInfo"];
[query setLimit: limit];
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
  if (!error) {
    // The find succeeded. Add the returned objects to allObjects
    [allObjects addObjectsFromArray:objects];
    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 findObjects... // Execute the query until all objects have been returned. Keep adding the results to the allObjects mutable array.

    }
  } else {
    // Log details of the failure
    NSLog(@"Error: %@ %@", error, [error userInfo]);
  }
}];