我试图获取当前登录用户正在关注的用户的数组,以便我可以在UICollectionView中显示它们。
我已经设置了一个活动类,其中包含指向用户的指针(在" fromUser"" toUser"列)。然后我尝试使用includeKey方法从User类获取每个用户的数据,这似乎不起作用。
这是我的代码:
- (void)queryParseForFollowingUsers {
// Create query for users being followed by the current user
PFQuery *followingUsers = [PFQuery queryWithClassName:@"activity"];
[followingUsers whereKey:@"fromUser" equalTo: [PFUser currentUser]];
[followingUsers whereKey:@"activityType" equalTo:@"follow"];
[followingUsers includeKey:@"toUser"];
[followingUsers findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// For each object from the query
for (PFObject *followedUser in objects) {
PFObject *userData = followedUser[@"toUser"];
// Add object to the array of followedUsers
[profilesArray addObject:userData];
[profilesCollection reloadData];
};
}];
NSLog(@"ARRAY RIGHT AFTER CREATION ######## %@", profilesArray);
}
奇怪的是,当我在块外部检查它的状态时,数组没有用任何内容更新,但是当我在循环中NSLog它的当前内容时它返回数据就好了。 profilesArray对象在.h文件中定义。
有没有人知道如何解决这个问题?我将不得不寻求任何帮助。
答案 0 :(得分:0)
我破解了它:
- (void)queryParseForFollowingUsers {
// Create query for users being followed by the current user
PFQuery *followingUsers = [PFQuery queryWithClassName:@"activity"];
[followingUsers whereKey:@"fromUser" equalTo: [PFUser currentUser]];
[followingUsers whereKey:@"activityType" equalTo:@"follow"];
[followingUsers includeKey:@"toUser"];
[followingUsers findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// Create an empty array for comparison
NSArray *emptyArray = [[NSArray alloc] init];
if ([objects isEqualToArray:emptyArray]) {
[profilesArray removeAllObjects];
// do something if there are no users followed by the current user
} else {
for (PFObject *followedUser in objects) {
PFObject *userData = followedUser[@"toUser"];
// Add object to the array of followedUsers
[profilesArray addObject:userData];
// Reload the collection view
[self.profilesCollection reloadData];
}
}
} else {
// Show the details of an error if it occurs
[[[UIAlertView alloc] initWithTitle:@"An error occured" message:[NSString stringWithFormat:@"%@", [error userInfo]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
}];
}