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

我是新手在这里在iOS和分析框架我想从我的分析表中从PFQuery中获取数据就像

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

谢谢。

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

您可以使用skip和limit参数为表格中的所有对象分页,方法是添加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]); } }]; 

我相信有可以获取的对象的数量限制。 我会做的是查询两个查询相同的事情,但对于第二个查询,做这样的事情

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

这是一个简单的recursion解决scheme,使用块从类中检索所有对象。

这是你最初的方式。

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

}

我用less于1000个对象,1000个对象和1000多个对象来testing它,它完美地工作。

请注意您抓取的对象有多less,因为这样会抓住所有对象,而且如果您正在处理一个可能成为内存问题的大型数据集,则会很快。