2014-11-20 62 views
0

我想獲得當前登錄用戶跟隨的用戶數組,以便我可以在UICollectionView中顯示它們。保存從解析查詢中的數據數組

我設置了一個活動類,其中包含指向用戶的指針(位於「fromUser」和「toUser」列)。然後我試着通過使用includeKey方法從User類中獲取每個用戶的數據,這似乎不起作用。

這裏是我的代碼:

- (void)queryParseForFollowingUsers { 

// Create query for users being followed by the current user 
PFQuery *followingUsers = [PFQuery queryWithClassName:@"activity"]; 
[followingUsers whereKey:@"fromUser" equalTo: [PFUser currentUser]]; 
[followingUsers whereKey:@"activityType" equalTo:@"follow"]; 
[followingUsers includeKey:@"toUser"]; 

[followingUsers findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 

    // For each object from the query 
    for (PFObject *followedUser in objects) { 

     PFObject *userData = followedUser[@"toUser"]; 

     // Add object to the array of followedUsers 

     [profilesArray addObject:userData]; 
     [profilesCollection reloadData]; 

    }; 

}]; 

NSLog(@"ARRAY RIGHT AFTER CREATION ######## %@", profilesArray); 

}  

奇怪的是,一個數組不更新任何內容,當我檢查塊之外的地位,但是當我的NSLog它在它返回的迴路電流內容數據就好了。 profilesArray對象在.h文件中定義。

有沒有人有任何線索如何解決這個問題?我很樂意提供任何幫助。

回答

0

我破解了:

- (void)queryParseForFollowingUsers { 

// Create query for users being followed by the current user 
PFQuery *followingUsers = [PFQuery queryWithClassName:@"activity"]; 
[followingUsers whereKey:@"fromUser" equalTo: [PFUser currentUser]]; 
[followingUsers whereKey:@"activityType" equalTo:@"follow"]; 
[followingUsers includeKey:@"toUser"]; 

[followingUsers findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    if (!error) { 

     // Create an empty array for comparison 
     NSArray *emptyArray = [[NSArray alloc] init]; 
     if ([objects isEqualToArray:emptyArray]) { 
      [profilesArray removeAllObjects]; 

      // do something if there are no users followed by the current user 

     } else { 

      for (PFObject *followedUser in objects) { 
       PFObject *userData = followedUser[@"toUser"]; 
       // Add object to the array of followedUsers 
       [profilesArray addObject:userData]; 
       // Reload the collection view 
       [self.profilesCollection reloadData]; 
      } 
     } 
    } else { 
// Show the details of an error if it occurs 
     [[[UIAlertView alloc] initWithTitle:@"An error occured" message:[NSString stringWithFormat:@"%@", [error userInfo]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; 
    } 

}]; 

}