如何在 iOS 中使用 PFQuery 获取 table 中的所有数据?
How to Fetch all data in a table using PFQuery in iOS?
我是 iOS 和 Parse Framework 的新手,我想从 PFQuery 的 Parse table 中获取数据,如
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 个对象,我想在这里获取我的所有 table 数据,它包含到 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]);
}
}];
谢谢。
我认为可以获取的对象数量存在查询限制。我会做的是查询对同一件事进行两次查询,但对于第二个查询,做这样的事情
[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]);
}
}];
https://parse.com/questions/fetch-all-data-in-a-table-using-pfquery
您可以使用 skip 和 limit 参数对 table 中的所有对象进行分页,方法是添加 limit 的值以跳过,直到查询 returns 小于 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]);
}
}];
这是一个简单的递归解决方案,使用块从 class 中检索所有对象。
这是您最初的称呼。
[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 个对象对此进行了测试,它运行良好。
请注意您抓取的对象数量,因为这会抓取所有对象,如果您处理的是大型数据集,这可能很快就会成为内存问题。
我是 iOS 和 Parse Framework 的新手,我想从 PFQuery 的 Parse table 中获取数据,如
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 个对象,我想在这里获取我的所有 table 数据,它包含到 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]);
}
}];
谢谢。
我认为可以获取的对象数量存在查询限制。我会做的是查询对同一件事进行两次查询,但对于第二个查询,做这样的事情
[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]);
}
}];
https://parse.com/questions/fetch-all-data-in-a-table-using-pfquery
您可以使用 skip 和 limit 参数对 table 中的所有对象进行分页,方法是添加 limit 的值以跳过,直到查询 returns 小于 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]);
}
}];
这是一个简单的递归解决方案,使用块从 class 中检索所有对象。
这是您最初的称呼。
[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 个对象对此进行了测试,它运行良好。
请注意您抓取的对象数量,因为这会抓取所有对象,如果您处理的是大型数据集,这可能很快就会成为内存问题。