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

Alf-Henning Drage
Alf-Henning Drage
3,573 Points

Ribbit - Have to restart app to update friends list

Hi, When I add or remove a friend I have to restart the app for it to update. When I access the data browser on Parse it updates immediately both when I add and remove a user. Wouldnt that mean that the problem is in the friendsViewController?

In the editViewController I use this else condition:

``` cell.accessoryType = UITableViewCellAccessoryCheckmark; [self.friends addObject:user]; [friendsRelation addObject:user];

So the app is adding friends to the array. 

Any advice would be great :-) 

My code for friendsViewController: 
http://pastebin.com/WXidkJXc

1 Answer

Daniel Templin
Daniel Templin
5,293 Points

This is due to the fact the PFQuery is running in the viewDidLoad method, which (once it's loaded), doesn't execute any code until it's left the devices memory and has to be completely reloaded. This is something that gets covered in more depth as your progress a little farther (I know I ran into similar issues and a few videos down the road I believe Ben pointed out this as the reason), when they go over the view life cycles.

The fix for this is to place the PFQuery code in the viewWillAppear method, since viewWillAppear executes each time you switch back to that view. Like I said it's covered considerably more in depth as you progress, but for the time being if you want it to refresh you would have something that looks like this;

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    PFQuery *query = [self.friendsRelation query];
    [query orderByAscending:@"username"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }
        else {
            self.friends = objects;
            [self.tableView reloadData];
        }
    }];
}
Alf-Henning Drage
Alf-Henning Drage
3,573 Points

Thanks! So at this stage the app isn't supposed to update when you navigate to the friends list? I just watched a video about viewWillAppear and viewDidAppear when clicking the camera-tab, so I should have thought about this myself ;-)