Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Robin Malhotra](https://uploads.teamtreehouse.com/production/profile-photos/902542/micro_IMAG0082.jpg)
Robin Malhotra
14,883 PointsParse.com returning Null
'''Objective-C -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:YES]; PFQuery *query=[[PFQuery alloc] initWithClassName:@"Messages"]; NSLog(@"%@",[[PFUser currentUser] objectId]); [query whereKey:@"recipientIDs" containsString:[[PFUser currentUser] objectId]]; [query orderByDescending:@"createdAt"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (error) { NSLog(@"%@",error.userInfo); } else { self.messages=[[NSArray alloc] initWithArray:objects]; [self.tableView reloadData]; NSLog(@"%lu",(unsigned long)[_messages count]); }
}];
}
-
(void)viewDidLoad { [super viewDidLoad]; PFUser *currentUser=[PFUser currentUser]; if (currentUser) { NSLog(@"logged in %@",currentUser.username); } else { [self performSegueWithIdentifier:@"showLogin" sender:self];
}
}'''
Here's my viewWillAppear code. I'm getting "0 objects" in the objects array.
3 Answers
![(Ryan) Tuan Phan](https://secure.gravatar.com/avatar/1915e70bdde1e0b2429833018e29cb7f?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
(Ryan) Tuan Phan
11,629 PointsThe method [[PFUser currentUser] objectId]] could return null if there is user logged in. So the fix is to add this condition in the viewWillAppear: -(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
PFUser* currentUser = [PFUser currentUser];
if (currentUser) // only do the query if the user is logged in
{
NSLog(@"Current user:%@", currentUser.username);
PFQuery* query = [PFQuery queryWithClassName:@"Messages"];
NSLog(@"Id: %@", [[PFUser currentUser] objectId]);
[query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray* objects, NSError* error)
{
if (error)
{
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else
{
//We found the messages
self.messages = objects;
[self.tableView reloadData];
}
}];
}
//current user is null, navigate to log in page else{ [self performSegueWithIdentifier:@"showLogin" sender:self]; }
![Roel Slüper](https://secure.gravatar.com/avatar/82889ef5f650ba8a78b5204d96e36d0a?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Roel Slüper
3,989 PointsYou can try to change [query whereKey:@"recipientIDs" containsString:[[PFUser currentUser] objectId]];
into [query whereKey:@"recipientIDs" equalTo:[[PFUser currentUser] objectId]];
. Also did you check if the error object created in the findObjectsInBackgroundWithBlock
call is empty of does it contain some error information?
![J Kim](https://secure.gravatar.com/avatar/a7bdd08e57110bb538cec7cf36c8b1ce?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
J Kim
15,218 PointsThis should be indicated in the video...
Diego Aguirre
13,211 PointsDiego Aguirre
13,211 PointsThanks so much for this Ryan! Funny thing with the code given by the video I can get to work in the simulator but as soon as I tried running it on my actual iPhone it broke like stated above.
To be more exact for some reason
[query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]];
it hated this code and no matter the different variations I tried of spelling recipientIds it would not work on the actual iPhone. Your if statement resolved this issue for me.