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

Szabolcs Varga
PLUS
Szabolcs Varga
Courses Plus Student 662 Points

TableView reloadData problem with Parse users and PFQuery

I have simple relation function in my app, users can add other users to their contact list like in Ribbit. Adding users to the relation works fine, the problem starts when i try to display the current users relations in a table view.

The problem is:

I launch the app from Xcode (i'm testing on iPhone device), (the root view controller is a blank view with a tab bar) then tap the button of the contact list on the tab bar, and nothing will be displayed. It's correct because there is no current user actually (i have an nslog in the viewDidLoad that writes out the current users name). Therefore i log out and sign in with an existing username, but in this case the nslog doesn't works when i'm in the contact list, but it should, because now there is a current user. So i'm stopping the app in Xcode, run it again and it's working well. My opinion is the problem is with the tableViewTwo's data reloading, it does not reloads normally, only when i stop and start running it again. I think it must be refreshed everytime when it's opened. Now i'm using the [tableViewTwo reloadData] but it works only when i launch the app again. Do you have any ideas how can i refresh it properly? Or what should i do to solve this issue? (I don't wanna use the PFQueryTableViewController.)

- (void)viewDidLoad
    {
        [super viewDidLoad];
        PFUser *current = [PFUser currentUser];
        NSLog(@"current user is: %@",current);
        self.simpleContact = [[PFUser currentUser] objectForKey:@"simpleContact"];

    }

    -(void) viewWillAppear:(BOOL)animated {


        [super viewWillAppear:animated];
        PFQuery *queryMyContacts = [self.simpleContact query];
        [queryMyContacts orderByAscending:@"username"];
        [queryMyContacts findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (error) {
                NSLog(@"Error %@ %@", error, [error userInfo]);
            }
            else {
                self.allMyContact = objects;
                [tableViewTwo reloadData];
            }
        }];
    }

    -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return [self.allMyContact count];
    }

    -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


        FriendsCell *cell = [tableViewTwo dequeueReusableCellWithIdentifier:@"friendsDevCell"];
        PFUser *user = [self.allMyContact objectAtIndex:indexPath.row];
        cell.myContactUsernameLabel.text = user.username;

    } ```

1 Answer

Thomas Nilsen
Thomas Nilsen
14,957 Points

You need to ensure that UIKit methods will be executed on the main thread. So when reloading the tableView, do this:

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self.tableView reloadData];
});

I'm not saying this will solve your problem, but something you should change either way

Szabolcs Varga
Szabolcs Varga
Courses Plus Student 662 Points

Thank you Thomas! Where should i place this code? I replaced the simple [tableViewTwo reloadData] with this code, but it doesn't solved the problem.