问题
My app has a view controller where the user can add friends. The app accesses the user's address book, and calls to the Parse.com backend server to see if any of the address book contacts are already using the app. If any are found, then I place the PFObjects for those users in the rows of my UITableView.
Each row in the table has a button. If the user presses a row's button, then the following code is executed which creates a PFRelation object called friendsRelation and adds the PFObject of the specific row that has been tapped:
PFRelation *friendsRelation = [self.currentUser relationforKey:@"friendsRelation"];
PFUser *user = [self.allUsersInParse objectAtIndex:indexPath.row];
[friendsRelation addObject:user];
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if(error) {
NSLog(@"%@ %@", error, [error userInfo]);
}
}];
This all works great for the most part. If I check the current user's row in Parse, and click on the "View Relations" button in their friendsRelation column/key, it will display the correct user's info that they just added.
However, if I go back to the main _User class, and find the row for the friend that was added by the current user, and click on their "View Relations" button in their friendsRelation column/key, it does not contain the user data for the current user that just added them.
So basically if you add someone to your friends list, they will show up on your friends list just fine, but if your new friend goes and looks at their friends list, you won't be on there at all.
I tried creating a fix by adding additional PFRelation code for the user being added by the current user. See user2:
PFRelation *friendsRelation = [self.currentUser relationforKey:@"friendsRelation"];
PFUser *user = [self.allUsersInParse objectAtIndex:indexPath.row];
NSLog(@"USER CONTENTS ON BUTTON PRESS: %@", user);
[friendsRelation addObject:user];
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if(error) {
NSLog(@"%@ %@", error, [error userInfo]);
}
PFUser *user2 = [self.allUsersInParse objectAtIndex:indexPath.row];
PFRelation *friendsRelation2 = [user2 relationforKey:@"friendsRelation"];
[friendsRelation2 addObject:self.currentUser];
[user2 saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error2) {
if(error2) {
NSLog(@"%@ %@", error2, [error2 userInfo]);
}
}];
This code is technically correct, but it is now allowed. Parse prints an error to the console and basically says that you are not allowed to add a PFRelation for a user that has not been authenticated via login or signup.
Does anyone have any ideas on how I can make this work?
The only thing i have thought of is that when the added friend logs in to the app, I can query Parse for any friendsRelation objects in the database that contain their objectId, and if any are found then I can create a new PFRelation for the currently logged in user.
But this will need to scan the entire database and seems like an inefficient fix that will be slow in the future as the database grows.
Thanks for the help.
回答1:
If you need to modify data on any user that is not the current user, you should use a CodeCloud function with Parse.Cloud.useMasterKey(); that way will bypass all ACL.
来源:https://stackoverflow.com/questions/22428785/ios-parse-com-pfrelation-for-adding-friends-only-adds-the-relationship-for-cur