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!

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

Luke Terzich
Luke Terzich
5,193 Points

Need Help with running a query after Login

Hello All, In my Ribbit App i Login with User1 and add 2 friends (user2 and user3)

When i logout i make a new account called user4 and login with this NEW USER.

If i view my friends list, i have the same friends as whoever was last logged in.

After 2-3 days of trial and error and thorough tests i have concluded that once you logout and log back in with a new user - the query on "FriendsViewController.m" is not being called.

I added

// Return the number of rows in the section. if([self.friends count] == 0) { NSLog(@"NO FRIENDS!"); }

and this doesn't run when i view the friends list meaning the array FRIENDS is equal to something (whoever was just logged in)

my question is WHY IS MY QUEERING NOT RUNNING? This is doing my head in! my code is as follows:

  • (void)viewDidLoad { [super viewDidLoad]; }

-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.friendsRelation = [[PFUser currentUser] objectForKey:@"friendsRelation"]; [self query_friends]; }

THIS IS THE QUERY:

  • (void)query_friends { 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];

        NSLog(@"count = %d", [_friends count]);
    
    }
    

    }]; }

thank you to anybody who can have a go at this!!!

UPDATE: Sorry forgot to mention that if i logout and log back in it all works fine. Its only the initial login after a new user signed up

2 Answers

Stone Preston
Stone Preston
42,016 Points

hmm interesting. To me it looks like it should be working fine. Not sure whats up really. Maybe try moving the line where you get the relation down into your query method.

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

    [self query_friends];
}


 - (void)query_friends
{

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

            NSLog(@"count = %d", [_friends count]);

        }
    }];
}
Luke Terzich
Luke Terzich
5,193 Points

Hello pal, just tried that and still doesn't work... I have put so many NSLogs in now its unreal!

I have a thought however, i mean tell me if I'm wrong but when i login i am "CURRENTUSER" and when i signup i am "NEWUSER"

PFUser *newUser =[PFUser user];
        newUser.username = username;
        newUser.password = password;
        newUser.email = email;
//inside SignupViewController.m
PFUser *currentUser = [PFUser currentUser];
    if(currentUser)
    {

    }
    else
    {
        [self performSegueWithIdentifier:@"showLogin" sender:self];
    }
//Inside InboxViewController.m

I do not know if i am looking too deep into this as I'm fairly new but i have a logical approach to all my work and i just cannot get past this one!

For some reason the query had been loaded (as i have an NSLOG in the method which gets shown in my Xcode console) the problem is it is grabbing any data from parse.com. The query isn't wrong because it works upon LOGIN (not SIGNUP)

Anything you can think of? :/