0

I think it's very trivial, but i can't figure it out why these codes are incorrect. I want to retrieve one image from the Parse User class. I mean, every user has a profile picture, that i store under the "imageFile" column and i would like to load it to a PFImageView. The upload works correctly, but i'cant retrieve it. The log says error, because there is no matches for the query. The file what i want to retrieve exists, so it's 100% that my query implementation is wrong. I would really appreciate any relevant help, thanks.

Non query version: (all versions are implemented in the viewDidLoad)

    self.userThumbnail.file = self.currentUser.imageFile;
    [self.userThumbnail loadInBackground];
  1. query version

      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];
        }
    }];
    
  2. query version

     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];
            }
        }];
    
rihekopo
  • 3,241
  • 4
  • 34
  • 63

1 Answers1

3

This line:

[queryImage whereKey:@"imageFile" equalTo:self.currentUser];

looks like a mistake. It says, "find Users whose imageFile column (presumably a PFImage) is equal to the current user object". That will never be true.

Try to separate the logic for finding a user from dealing with it's imageFile. In other words. First get a user:

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) {}];

Now, inside the completion block for querying a user, do your file logic:

[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;
        }
    }];
}];
danh
  • 62,181
  • 10
  • 95
  • 136
  • Thank you danh for spending time to help me. Fortunately i've just solved it with an easier solution. `PFFile *file = [self.currentUser objectForKey:@"imageFile"]; self.userThumbnail.file = file;` In this case we don't have to make a query. But i think your implementation is also correct, but my opinion is the first part doesn't necessary if we want the current user (as in my case), but sure if we want an another user's imageFile it can be the way. I think i will use this block in the block solution elsewhere. – rihekopo Jun 01 '14 at 16:40
  • @JoeBlow - do you mean retain cycles in the blocks? I don't think we're exposed to that in the above. – danh Jun 01 '14 at 16:57