2014-06-12 35 views
1

我一直在試圖通過研究和犁過幾天的文檔來弄明白這一點,我無法得到它/我不明白它。Parse.com - 編寫關係查詢?

這裏就是我想要實現:

enter image description here

我有一個名爲「ProfilePhotos」自定義類和一個標準的用戶級。 當我將圖像轉儲到名爲「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; 


} 
+0

您必須爲配置文件映像需要一個分隔類嗎?你想存儲以前的個人資料圖片? – rihe

+0

我確實需要兩個類,因爲我需要知道圖像的createdAt日期並創建了用戶的數據(單獨)。另外,不把所有東西都放在一個巨人班上是不是更好的做法? – Aubrey

+0

在你的User類中製作怪物並不是一個好習慣,但我認爲在這種情況下,從那裏存儲和檢索配置文件圖像要容易得多。通常您會顯示用戶的最近配置文件映像,因此不需要createdAt。但是,如果要存儲用戶的以前的個人資料圖像,請使用不同的課程,但是如果您只需要最新的圖像,則她將其作爲個人資料圖像上傳到用戶級別。 – rihe

回答

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

優秀。這不僅是它,它爲我學習了烤肉。現在我知道如何做到這一點,多虧了你。 **帽子提示**蒂莫西。十分感謝。 – Aubrey

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

所以如果我想從用戶類獲得一個名爲「名稱」(不是用戶名)的列是:[查詢whereKey:@「user」equalTo:「Name」]; ? – Aubrey

+0

如果您想要名稱列中的一個確切用戶,例如當前用戶執行user.Name或user.name,因爲您需要一個字符串,並且Name是User類的字符串屬性(但聲明當前用戶對象稱爲用戶)。 '[whereKey:@「user」equalTo:user.Name];' – rihe

+0

現在試試這個 – Aubrey