我有一个状态的Parse查询,现在它返回当前用户在过去24小时内跟随新的状态的所有状态。如您所见,我使用NSMutableArray
来获取以下用户,然后查询其状态。
我希望将其限制为每个用户只有1个状态,因此当用户更新状态时,它会替换旧状态。
此外,如果我可以将刷新的顺序随机化,那将很酷。
-(void)getFollowingStatus {
PFQuery *following = [PFQuery queryWithClassName:@"Phriends"];
[following whereKey:@"followeds" equalTo:[PFUser currentUser].username];
[following findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
NSMutableArray *followingUsers = [[NSMutableArray alloc] init];
for (PFObject *followingActivities in objects) {
PFUser *user = [followingActivities objectForKey:@"following"];
[followingUsers addObject:user];}
NSDate* CurrentTime = [NSDate date];
NSDate *newDate = [CurrentTime dateByAddingTimeInterval:-86400];
PFQuery *feed = [PFQuery queryWithClassName:@"Pheed"];
[feed whereKey:@"user" containedIn:followingUsers];
[feed whereKey:@"createdAt" greaterThan:newDate];
[feed orderByDescending:@"createdAt"];
[feed findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
//
if (!error) {
self.wallObjectsArray = objects;
[self loadWallViews];
} else {
[[[UIAlertView alloc] initWithTitle:@"Error" message:[error userInfo][@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
}];
}];
}
答案 0 :(得分:2)
您可以通过设置限制来限制结果数量。默认情况下,结果限制为100,但1到1000之间的任何值都是有效限制:
query.limit = 10; // limit to at most 10 results
如果您只想要一个结果,更方便的替代方法可能是使用getFirstObject
或getFirstObjectInBackground
而不是findObject
。
PFQuery *query = [PFQuery queryWithClassName:@"GameScore"];
[query whereKey:@"playerEmail" equalTo:@"dstemkoski@example.com"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!object) {
NSLog(@"The getFirstObject request failed.");
} else {
// The find succeeded.
NSLog(@"Successfully retrieved the object.");
}
}];
答案 1 :(得分:0)
不幸的是,您无法使用limit
查询参数来限制数组属性的大小。我建议做这样的事情:
[feed findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSMutableArray * statuses = [NSMutableArray new];
for (PFUser * user in objects)
[statuses addObject:[user.statuses firstObject];
self.wallObjectsArray = [statuses copy];
[self loadWallViews];
} else {
[[[UIAlertView alloc] initWithTitle:@"Error" message:[error userInfo][@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
}];
希望我能正确理解你的情况。基本上,您需要构建一个新数组,在该数组中,您只从每个用户获取第一个状态。