我的应用程序将不得不经常抓取任意数量的用户的UserID(假设被抓取的随机UserID的数量在1到100之间)。
我能想到的每一个解决方案都涉及count
,这对我(有限的)理解而言非常昂贵且缓慢。
我不需要返回整行,只需要UserID(本例中为主键)。什么方法是最快和最具规模的?有没有办法可以利用只查询索引,因为我唯一需要的是主键?
假设该表名为Users,主键当然是UserId。表中的用户数量可以是数百万。
答案 0 :(得分:0)
好的,因为你一次只能搜索1000,所以这是我返回随机数量的用户对象的解决方案。你的班级名称会有所不同..
-(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);
}
}];
}
}];
}