gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:32:52 24 4
gpt4 key购买 nike

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

谢谢。

最佳答案

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

[查询 setSkip1000];

您可以跳过第一个查询中的前 1000 个对象并获取接下来的 1000 个对象。将您的数组设为 NSMutableArray 并在每个 block 内执行此操作

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

关于ios - 如何在 iOS 中使用 PFQuery 获取表中的所有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28712545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com