我是iOS和的新手,我想从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]);
}
}];谢谢。
发布于 2015-02-25 06:50:00
https://parse.com/questions/fetch-all-data-in-a-table-using-pfquery
可以使用“跳过和限制”参数对表中的所有对象进行分页,方法是添加要跳过的限制值,直到查询返回小于极限的对象数量。
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]);
}
}];发布于 2015-02-25 06:45:25
我相信可以获取的对象数量有一个查询限制。我要做的是对同一件事进行两个查询,但是对于第二个查询,执行如下所示的操作
[query setSkip1000];
您可以跳过第一个查询中的前1000个对象,然后获取下一个1000个对象。使数组成为一个NSMutableArray,并在每个块内执行以下操作
[self.myArray addObjects:objects];,而不是self.myArray = objects;,这样就覆盖了数组中的对象。
编辑
而不是两个单独的查询,您也可以这样做
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]);
}
}];发布于 2016-06-28 18:54:27
这是一个简单的递归解决方案,可以使用块从类中检索所有对象。
这是你最初怎么称呼它的。
[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多个对象,它运行得很好。
但是,要小心抓取多少对象,因为这会捕获所有对象,如果您正在处理大型数据集,这可能很快就会成为内存方面的一个问题。
https://stackoverflow.com/questions/28712545
复制相似问题