Aubrey
Aubrey

Reputation: 629

Parse.com - writing relational queries?

I've been trying to figure this out via research and plowing through the docs for a few days and I can't get it / I don't understand it.

Here's what I'm trying to achieve:

enter image description here

I have a custom class named "ProfilePhotos" and a standard User class. I have some code that is "working" when I dump images onto the main User class in a column called "profilePicture". I need to get the image from 'imageFile' in the ProfilePhotos class.

I CAN figure out how to create images into one class (ProfilePhotos), but CAN'T figure out how to call them up while gathering data from the User to whom the image belongs ("Name" field).

The code I have that works, but has some weird bugs (images load in the wrong order, not at all, crashes) is below:

@interface CollectionViewController () <UITextViewDelegate>

@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong)  PFObject *theName;

@end

@implementation CollectionViewController

- (instancetype) init { //INIT STUFF }
-(void) viewDidLoad { //VIEWDIDLOAD STUFF }

-(void) DataCalls {

    PFQuery *getPhotos = [PFUser query];
    [getPhotos findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            NSLog(@"Objects Are:%@", objects);

            _imageArray = [[NSMutableArray alloc] initWithArray:objects];

            for (PFObject* photo in objects  ) {
                PFFile *pictureFile = [photo objectForKey:@"profilePicture"];
                _theName = [photo objectForKey:@"Name"];
                [self.collectionView reloadData];
                NSLog(@"Name is:%@", _theName);
                [pictureFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                    if (!error) {

                        NSLog(@"Fetching image..");
                        [_imageArray addObject:[UIImage imageWithData:data]];
                        NSLog(@"Size of the _imageArray : %lu", (unsigned long)[_imageArray count]);

                    } else {
                        // Log details of the failure
                        NSLog(@"Error: %@ ", error);
                    }
                }];

            }
        }
    }];    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photo" forIndexPath:indexPath];

    _imageView = (UIImageView *)[cell viewWithTag:200];


    PFObject *imageObject = [_imageArray objectAtIndex:indexPath.row];
    PFFile *imageFile = [imageObject objectForKey:@"profilePicture"];

    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (!error) {
            _imageView.image = [UIImage imageWithData:data];

        }
    }];

    PFObject *nameText = [imageObject objectForKey:@"Name"];
    NSLog(@"Name in the CV:%@", nameText);
    cell.userName.text= imageObject[@"Name"];

    return cell;


}

Upvotes: 1

Views: 392

Answers (2)

Timothy Walters
Timothy Walters

Reputation: 16874

Keep using pointers, don't switch to using username to link things. Just remember to use includeKey: on your query to return fully populated pointers, e.g.:

PFQuery *query = [PFQuery queryWithClassName:@"ProfilePhotos"]

// apply any filters, here's how to filter by user:
// could just as easily be another user
PFUser *user = [PFUser currentUser];
[query whereKey:@"user" equalTo:user];

// this allows reading of user properties in the results
[query includeKey:@"user"];

[query orderByDescending:@"createdAt"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    if (error) {
        NSLog(@"Error.");
    } else {
        PFFile *file = object[@"imageFile"];
        PFUser *pictureUser = object[@"user"];
        NSString *name = pictureUser[@"Name"];
    }
}];

Upvotes: 1

rihekopo
rihekopo

Reputation: 3350

Try out this, it should work.

PFQuery *query = [PFQuery queryWithClassName:@"ProfilePhotos"];
[query whereKey:@"user" equalTo:desiredUsername];
[query orderByDescending:@"createdAt"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    if (error) {
        NSLog(@"Error.");
    } else {
        PFFile *file = [object objectForKey:@"imageFile"];
        //Now you can pass it to the image view to display the image
    }
}];

Upvotes: 0

Related Questions