Request to get BOOL from Parse DB for the current logged in user which will determine which segue will go to

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

While the user is creating an account, the logical profile is completed is automatically assigned NO , since the user has not yet finished creating his profile. (This happens in RegisterViewController.m).

When they are registered, they navigate 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 is that I want a query (when the user is logged in) to check if the BOOL profileCompleted is set to True or False. I've checked the Parse documentation but just can't get it to work.

Whether BOOL is true or false will determine that the segue will proceed to the next. (If profileCompleted = true, they will be transferred to the main page. If profileCompleted = false, they will be sent to the profile editor).

EDIT2:

Now my code 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 always get "Profile was not completed" in my output.

EDIT3:

The only place in my code where profileCompleted gets assigned 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){
    ...

      

+3


source to share


1 answer


You can simply get the value profileCompleted

from the current user -

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

if (profileCompleted) {
   ...
}

      



if the data stored in PFUser might be out of date (for example, the profile was populated elsewhere), then you will need to retrieve 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) {
           ...
        }
    }
  }];
}

      

+1


source







All Articles