Parse.com - 编写关系查询?

时间:2014-06-12 16:23:22

标签: objective-c xcode uicollectionview parse-platform pfquery

我一直在试图通过研究和浏览文档来解决这个问题几天,我无法理解它/我不理解它。

这就是我想要实现的目标:

enter image description here

我有一个名为“ProfilePhotos”的自定义类和一个标准的User类。 当我将图像转储到名为“profilePicture”的列中的主User类时,我有一些“工作”的代码。我需要在ProfilePhotos类中从'imageFile'获取图像。

我可以弄清楚如何将图像创建到一个类(ProfilePhotos)中,但是无法弄清楚如何在从图像所属的用户收集数据时调用它们(“名称”字段)。

我的代码有效,但有一些奇怪的错误(图像加载顺序错误,完全没有,崩溃)如下:

@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;


}

2 个答案:

答案 0 :(得分:1)

继续使用指针,不要切换到使用username链接内容。请记住在查询中使用includeKey:来返回完全填充的指针,例如:

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

答案 1 :(得分:0)

试试这个,它应该有用。

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