Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.