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

Georgi Zlatinov
Georgi Zlatinov
3,191 Points

I am trying to do the Extra Credit task for Ribbit. I got a bug and i cannot figure out how to resolve it.

Once I tap on a friend, i log into the console the user id, from didSelectRow, then I assign this value to userID property that is part of the ProfileViewController, once I try to log it into the console the first time i always get (null) and if i go back and tap again i get the value of userID. And if i try to tap on another user then the first NSLog from didSelectRowAtIndex prints out the correct objectId but when I try to print it from my ProfileViewController I get the objectID of the friend that I had previously tapped on. (e.g. I always get one step behind when I try to print the objectId from ProfileViewController)

Here is my FriendsViewController.h

@property (nonatomic, strong) PFRelation *friendsRelation; @property (nonatomic, strong) NSArray *friends; @property (nonatomic, strong) NSString *selectedUserID;

FriendsVIewController.m

  • (void)viewDidLoad { [super viewDidLoad];

    self.friendsRelation = [[PFUser currentUser] objectForKey:@"friendsRelation"]; 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]; } }]; }

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

    // get the user that was tapped on PFUser *user = [self.friends objectAtIndex:indexPath.row]; self.selectedUserID = user.objectId; NSLog(@"user tapped on ID:%@",self.selectedUserID); }

  • (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([segue.identifier isEqualToString:@"showEditFriends"]){ EditFriendsTableViewController *viewController = (EditFriendsTableViewController *)segue.destinationViewController; viewController.friends = [NSMutableArray arrayWithArray:self.friends]; } else if ([segue.identifier isEqualToString:@"showProfileView"]){

    ProfileViewController *viewController = (ProfileViewController *)segue.destinationViewController;
    viewController.userID = self.selectedUserID;
    

    } }

ProfileViewController.h

@property (nonatomic, strong) NSString *userID; @property (nonatomic, strong) NSString *test; @property (nonatomic, strong) NSArray *userDetails; @property (weak, nonatomic) IBOutlet UILabel *usernameLabel; @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @property (weak, nonatomic) IBOutlet UILabel *locationLabel; @property (weak, nonatomic) IBOutlet UILabel *mobileLabel;

ProfileViewController.m

  • (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view NSLog(@"user id: %@", self.userID);

    PFQuery *query = [PFUser query];
    [query whereKey:@"objectId" equalTo:self.userID];
    self.userDetails = [query findObjects];
    NSLog(@"array %@", girls);
    self.usernameLabel.text = self.userID;
    

    }

2 Answers

Diego Auza
Diego Auza
3,501 Points

i have the same problem! its a problem with the query because when we first enter to the app the query of the friend takes too long to charge and the tableviewcontroller is already charged so the friends gets a value of null even if is in the screen the list, so maybe the query have to be fast or the tableviewcontroller has to charge slower, i searching for the solution but i dont found anything...

Georgi Zlatinov
Georgi Zlatinov
3,191 Points

I spent a lot of time on researching that, tried many things online but without success. don't spend too much time on that

Georgi Zlatinov
Georgi Zlatinov
3,191 Points

just found the answer!!!

PFQuery *query = [PFUser query]; [query whereKey:@"objectId" equalTo:self.userID]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if(error){ NSLog(@"Error: %@ %@", error, [error userInfo]); } else { for (PFObject *object in objects) { NSLog(@"%@", object.objectId); NSLog(@"username: %@", object[@"username"]); NSLog(@"mobile: %@", object[@"mobile"]);
self.usernameLabel.text = object[@"username"]; self.mobileLabel.text = object[@"mobile"];
}

        }

}];

hope this helps!

Diego Auza
Diego Auza
3,501 Points

its still doesnt working becuause when the table view controller of friends is loaded the query is still loading so the friends are null...