0

I have simple relation function in my app, users can add other users to their contact list. 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.)

My code:

- (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;

 }
Larme
  • 24,190
  • 6
  • 51
  • 81
rihekopo
  • 3,241
  • 4
  • 34
  • 63
  • 1
    You can refresh UI only in Main Thread. And in your code, you try it in background. Look there: http://stackoverflow.com/questions/7914299/ios-another-thread-needs-to-send-reloaddata-to-the-mainthread – Larme May 19 '14 at 10:46
  • @Larme in the linked question there's no details about where should i use that code. Is it ok if i put it in the `numberOfRowsInSection`? Or i should create a new method for it? – rihekopo May 19 '14 at 10:59
  • You're doing `findObjectsInBackgroundWithBlock`, and inside its completion block, you're doing the `reloadData`. So it's in background, and you can't. So you need to replace you `[tableViewTwo reloadData];` with the accepted answer in the linked question. – Larme May 19 '14 at 11:01
  • @Larme unfortunately it doesn't works. I tried it inside the `findObjectsInBackgroundWithBlock` and also outside but it's still the same. Do you have any other idea? – rihekopo May 19 '14 at 11:17

0 Answers0