Ghobs
Ghobs

Reputation: 859

How to make a property of a Parse Installation object be a pointer to a User object?

Currently, when a user opens my Parse app for the first time, a new Installation object is created on the Parse database with their installationId and other properties.

Once they log in, it updates this Installation object's userId property with the objectId string of that person's User object. It's nothing more than a string that happens to be equivalent to the respective Users objectId though, what I want is for it to also be a pointer to that User object.

I've tried setting the userId property to be a pointer instead of a string from the website, but this gives me an error from the app stating Error: invalid type for key userId, expected object, but got string, because my iOS code is sending it over as a string, and not as a pointer to the User class.

How can I make the userId string point to the User class from my code? The docs are here, but I'm having trouble applying it to this use case.

Code:

- (void)viewWillAppear:(BOOL)animated {
    [super viewDidLoad];
    if ([PFUser currentUser]) {
        NSLog(@"User is logged in");

        PFInstallation *currentInstallation = [PFInstallation currentInstallation];
        NSString *userId = [PFUser currentUser].objectId;

        //userId[@"parent"] = [PFUser currentUser];

        [currentInstallation setObject:userId forKey: @"userId"];
        [currentInstallation saveInBackground];

    } else {
        NSLog(@"User is not logged in");
        //[welcomeLabel setText:@"Not logged in"];
    }
}

Upvotes: 0

Views: 762

Answers (1)

Dhwanit Zaveri
Dhwanit Zaveri

Reputation: 475

The way to store a point to an object in parse is by saving the entire object to it. So don't store the userId in an NSString. Simply do the following:

- (void)viewWillAppear:(BOOL)animated {
    [super viewDidLoad];
    if ([PFUser currentUser]) {
        NSLog(@"User is logged in");

        PFInstallation *currentInstallation = [PFInstallation currentInstallation];
        PFUser* user  = [PFUser currentUser];

        [currentInstallation setObject:user forKey: @"userId"];
        [currentInstallation saveInBackground];

    } else {
        NSLog(@"User is not logged in");
        //[welcomeLabel setText:@"Not logged in"];
    }   
}

This will cause parse to save a pointer to the user as you want.

Upvotes: 1

Related Questions