【问题标题】:Parse.com - writing relational queries?Parse.com - 编写关系查询?
【发布时间】:2014-06-12 16:23:22
【问题描述】:

几天来,我一直试图通过研究和阅读文档来解决这个问题,但我无法理解/不明白。

这是我想要达到的目标:

我有一个名为“ProfilePhotos”的自定义类和一个标准用户类。 当我将图像转储到名为“profilePicture”的列中的主用户类时,我有一些正在“工作”的代码。我需要从 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;


}

【问题讨论】:

  • 您必须为个人资料图像设置一个单独的类?您想存储以前的个人资料图片吗?
  • 我确实需要两个类,因为我需要知道图像的 createdAt 日期和用户的 createdAt 数据(分别)。另外,不把所有东西都放在一个大类上不是更好的做法吗?
  • 从你的用户类中创建一个怪物不是一个好习惯,但我认为在这种情况下,从那里存储和检索个人资料图像要容易得多。通常您会显示用户最近的个人资料图片,因此不需要 createdAt。但是,如果您想存储用户以前的个人资料图片,请使用不同的类,但如果您只需要她作为个人资料图片上传的最新图片,请使用 User 类。
  • createdAt 为个人资料图像是必需的。我能够从主用户类制作和检索配置文件图像没有问题。如果您知道如何在获取相关用户数据的同时在他们自己的班级中制作个人资料图片,我很想看看如何去做。那是我的挂断电话:)

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


【解决方案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"];
    }
}];

【讨论】:

  • 优秀。不仅是这样,它让我学到了知识。多亏了你,我现在知道该怎么做。 帽子提示蒂莫西。非常感谢。
【解决方案2】:

试试这个,应该可以的。

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

【讨论】:

  • 所以如果我想从用户类中获取一个标有“名称”(不是用户名)的列,是这样的:[query whereKey:@"user" equalTo:"Name"]; ?
  • 如果您想要 Name 列中的一个确切用户,例如当前用户执行 user.Name 或 user.name,因为您需要一个字符串,而 Name 是 User 类的字符串属性(但声明一个名为 user 的当前用户对象)。 [query whereKey:@"user" equalTo:user.Name];
  • 马上试试这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-23
  • 1970-01-01
相关资源
最近更新 更多