Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query to retrieve a BOOL from Parse DB for the current logged in user, which will determine what segue to go to

I'm trying to create a Query that will access a BOOL from the current logged in user's data from my Parse database.

So far, when the user creates an account, the boolean profileCompleted is automatically assigned to NO as the user has not finished creating their profile. (This takes place in RegisterViewController.m).

When they have signed up, they are taken to the root navigation controller (FindFriendsViewController.m). Now from there, it checks if the user is logged in,

- (void)viewDidLoad {
[super viewDidLoad];

PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
    //The User is logged in
    NSLog(@"Current user: %@", currentUser.username);
}
else {
    //The User is not logged in
    [self performSegueWithIdentifier:@"showLogin" sender:self];
}
}

Now, the problem I am having is I want a query (when a user is logged in) to check if their BOOL value profileCompleted is set to True or False. I have checked the Parse documentation, but just can't get it to work.

Whether the BOOL is true or false will determine what segue to go to next. (If profileCompleted = true, they will be taken to the home page. If profileCompleted = false, they will be taken to the profile editor).

EDIT2:

My code now looks like this:

- (void)viewDidLoad {
[super viewDidLoad];

PFUser *currentUser = [PFUser currentUser];
BOOL profileCompleted = [currentUser[@"profileCompleted"] boolValue];

if (currentUser) {
    NSLog(@"Current user: %@", currentUser.username);

    if (profileCompleted){
        NSLog(@"Profile has been completed");
    }
    else {
        NSLog(@"Profile has not been completed");
    }

}
else {
    //Then User is not logged in
    [self performSegueWithIdentifier:@"showLogin" sender:self];
}

}

No matter what the values are in the database (True or False), I'm always getting "Profile has not been completed" on my output.

EDIT3:

The only place in my code where profileCompleted gets assigned a value is in my RegisterViewController.m file:

    ...
    PFUser *newUser = [PFUser user];
    newUser.username = username;
    newUser[@"firstName"] = firstName;
    newUser[@"surname"] = surname;
    newUser.password = password;
    newUser.email = email;
    newUser[@"profileCompleted"] = @NO; <--- HERE

    [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error){
    ...
like image 915
CrackerBarrelKid55 Avatar asked Nov 17 '25 18:11

CrackerBarrelKid55


1 Answers

You can just retrieve the profileCompleted value from the current user -

PFUser *currentUser = [PFUser currentUser];
BOOL profileCompleted= [[currentUser[@"profileCompleted"] boolValue];

if (profileCompleted) {
   ...
}

if the data held in PFUser may be stale (e.g. profile was completed somewhere else) then you will need to fetch the user -

PFUser *currentUser = [PFUser currentUser];
if (currentUser != nil) {
    [currentUser fetchInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    if (error == nil) {
        BOOL profileCompleted= [[object[@"profileCompleted"] boolValue];

        if (profileCompleted) {
           ...
        }
    }
  }];
}
like image 100
Paulw11 Avatar answered Nov 19 '25 10:11

Paulw11



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!