gpt4 book ai didi

ios - 从我的 Parse 数据库中获取任意数量的 UserID 的最快方法

转载 作者:搜寻专家 更新时间:2023-11-01 05:40:11 25 4
gpt4 key购买 nike

我的应用程序必须经常获取任意数量用户的 UserID(假设要获取的随机 UserID 的数量在 1 到 100 之间)。

我能想到的每个解决方案都涉及 count,根据我(有限)的理解,这非常昂贵且缓慢。

我不需要返回整行,只需要返回 UserID(在本例中是主键)。哪种方法最快且最具可扩展性?有没有一种方法可以利用仅查询索引的优势,因为我真正需要的唯一东西就是主键?

假设表名为Users,主键当然是UserId。表中的用户数量可以达到数百万。

最佳答案

好的,既然您一次只能搜寻 1000 个,下面是我返回随机数量的用户对象的解决方案。你的类(class)名称会有所不同..

-(IBAction)findAllUsers:(id)sender{

//Random int between 1-1000
int randomInt = arc4random() % 1000;

NSMutableArray *allObjects = [NSMutableArray array];

NSUInteger limit = randomInt;

NSLog(@"Random User Count: %lu",(unsigned long)limit);

//Keep going to the end of the objects
__block NSUInteger skip = 0;

PFQuery *query = [PFQuery queryWithClassName:@"YourMainClass"];
[query selectKeys:@[@"UserId"]];
[query setLimit: limit];
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

// The find succeeded. Add the returned objects to allObjects

NSLog(@"Found %lu users", (unsigned long)objects.count);
[allObjects addObjectsFromArray:objects];
if (objects.count == limit) {

skip += limit;
[query setSkip: skip];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// Keep going until no objects left to find
if (!error) {
[allObjects addObjectsFromArray:objects];
}
else{
NSLog(@"Error: %@",error);
}

}];
}
}];

}

关于ios - 从我的 Parse 数据库中获取任意数量的 UserID 的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31617145/

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