rihekopo
rihekopo

Reputation: 3350

Query PFFile from the pre-defined User class with Parse

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

Upvotes: 0

Views: 485

Answers (1)

danh
danh

Reputation: 62676

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

Upvotes: 3

Related Questions