Parse editing object after relational query

166 Views Asked by At

In my application the user creates a PFObject in the alarm class that has a relation to the current user. I would like to retrieve it and edit it.

Adding the object:

PFUser *user = [PFUser currentUser];
PFObject *alarm = [PFObject objectWithClassName:@"Alerts"];
alarm[@"Active"] = @YES;
alarm[@"ActionWasCompleted"] = @NO;
alarm[@"Time"] = _alarmTime;

PFRelation *relation = [user relationForKey:@"user"];
[relation addObject:user];
[alarm saveInBackground];

Retrieving the user specific object and editing it:

PFQuery *query = [PFQuery queryWithClassName:@"Alerts"];
[query whereKey:@"user" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        NSLog(@"Successfully retrieved %d objects.", objects.count);
        [[objects objectAtIndex:0] setObject:@YES forKey:@"ActionWasCompleted"];
    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

Now I have used relational queries before I haven't edited them, but I cant even seem to retrieve the objects in the alarm class related to the current user. Let alone edit the object. I don't know what I'm doing wrong.

All I would like to do is create a PFObject that is specific to the user, retrieve it and edit it.

If you have any suggestions I'm all ears! Thanks!

EDIT: I figured out how to save the queried object using

[PFObject saveAllInBackground:objects];

But I'm still unable to retrieve objects related to the current user.

1

There are 1 best solutions below

2
On BEST ANSWER

Looking at your Alerts creation code, you have a 1:m relationship between User->Alerts (oddly, via the PFUser's user field), but no relationship from Alerts->User (or if you do, it's not set). You can still query all Alerts for a given User, but you have to do so from querying via the User, rather than via the Alerts.

Also, you might want to check the relationship - it looks like your PFUser has a field called "user" which you're using as a relationship to the Alerts. I'd recommend renaming that to Alerts, just so it's clear what the relationship is to.

PFRelation *alertsRelation = [[PFUser currentUser] relationForKey:@"user"]; // Should rename the relation on the user so it's clear what it's a relation to
PFQuery *alertsQuery = [alertsRelation query];
[alertsQuery findObjectsInBackgroundWithBlock:^(NSArray *alerts, NSError *error) {
  // alerts now contains the the Alerts for the current PFUser
}];

Just another thought - if you're expecting your relationship to the user is being set on the Alert via a relation field with the name user, you should change your object adding code from

PFRelation *relation = [user relationForKey:@"user"];
[relation addObject:user];
[alarm saveInBackground];

to

PFRelation *relation = [alarm relationForKey:@"user"];
[relation addObject:user];
[alarm saveInBackground];

If you do this, then your original query code should work, as the user relationship on the Alerts is now being set.