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

iOS Build a Self-Destructing Message iPhone App Retrieving and Viewing Data from Parse.com Retrieving Data from Parse.com

Robin Malhotra
Robin Malhotra
14,883 Points

Parse.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
(Ryan) Tuan Phan
11,629 Points

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

Diego Aguirre
Diego Aguirre
13,211 Points

Thanks 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.

Roel Slüper
Roel Slüper
3,989 Points

You 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
J Kim
15,218 Points

This should be indicated in the video...