我认为这非常简单,但我无法弄清楚为什么这些代码不正确。 我想从Parse User类中检索一个图像。我的意思是,每个用户都有一个个人资料图片,我存储在“imageFile”列下,我想将它加载到PFImageView。上传工作正常,但我不能检索它。日志显示错误,因为查询没有匹配项。我想要检索的文件存在,所以100%我的查询实现是错误的。我非常感谢任何相关的帮助,谢谢。
非查询版本:(所有版本均在viewDidLoad
中实施)
self.userThumbnail.file = self.currentUser.imageFile;
[self.userThumbnail loadInBackground];
查询版本
PFQuery *queryImage = [PFQuery queryWithClassName:@"User"];
[queryImage whereKey:@"imageFile" equalTo:self.currentUser]
[queryImage getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (error) {
NSLog(@"ERROR");
} else {
PFFile *file = [object objectForKey:@"imageFile"];
self.userThumbnail.file = file;
[self.userThumbnail loadInBackground];
}
}];
查询版本
PFQuery *queryImage = [PFUser query];
[queryImage whereKey:@"imageFile" equalTo:self.currentUser];
[queryImage getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (error) {
NSLog(@"ERROR");
} else {
PFFile *file = [object objectForKey:@"imageFile"];
self.userThumbnail.file = file;
[self.userThumbnail loadInBackground];
}
}];
答案 0 :(得分:3)
这一行:
[queryImage whereKey:@"imageFile" equalTo:self.currentUser];
看起来像个错误。它说,“找到其imageFile列(可能是PFImage)等于当前用户对象的用户”。那将永远不会成真。
尝试将寻找用户的逻辑与处理imageFile分开。换一种说法。首先得到一个用户:
PFQuery *queryUser = [PFQuery queryWithClassName:@"User"];
[queryUser whereKey:@"username" equalTo:@"some-user-name"];
[queryUser getFirstObjectInBackgroundWithBlock:^(PFObject *user, NSError *error) {}];
// or maybe instead of username==, get a user with their id ...
[queryUser getObjectInBackgroundWithId:@"some-user-id" block:^(PFObject *user, NSError *error) {}];
现在,在查询用户的完成块内,执行文件逻辑:
[queryUser getFirstObjectInBackgroundWithBlock:^(PFObject *user, NSError *error) {
PFFile *userImageFile = user[@"imageFile"];
[userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:imageData];
// this is a regular UIImage now, put it in your UI
self.someUIImageView.image = image;
}
}];
}];