1

So in my app I am using Parse for user accounts. I have a class "Follow" with "to" and "from" fields containing user object ids, allowing users to follow each other. Now, if I delete a user somehow, the follow relation remains in tact and querying empty user data raises an object not found error. What I want to know is how I can delete a follow relation if any of the "to" or "from" fields contains an objectID of an object that does not exist.

I have tried querying objects by their objectID, but any attempt at checking empty data (like checking if a user.username is nil) has resulted in a missing object error, and I can't check if an object is nil because Xcode says it never will be.

Thanks!

rici
  • 234,347
  • 28
  • 237
  • 341
NotMe
  • 77
  • 11
  • are you delete the user manually? – Jaimish Jan 08 '16 at 04:49
  • yes I am, but I was asking just in case I wanted to implement a deleteUser() method. But I guess in that case I could remove any associated follow relations before deleting the user. So I probably shouldn't worry about this right? – NotMe Jan 08 '16 at 05:02
  • for that you have to use cloud code.....implement the before delete trigger – Jaimish Jan 08 '16 at 05:12

1 Answers1

2

I think you need to acquire it manually, if you have deleted user, then you can delete all of its relations to maintain Database integrity, following is the code you would calling after deleting any User.

PFQuery * _to = [PFQuery queryWithClassName:@"Follow"];
[_to whereKey:@"to" equalTo:@"Replace UserID of Deleted User"];

PFQuery * _from = [PFQuery queryWithClassName:@"Follow"];
[_from whereKey:@"from" equalTo:@"Replace UserID of Deleted User"];

PFQuery *query = [PFQuery orQueryWithSubqueries:@[_to, _from]];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
  if (!error) {
     // The find succeeded.
     NSLog(@"Successfully retrieved %d relations.", results.count);
     // Do something with the found objects, lets delete them all to intact relationship
     [PFObject deleteAllInBackground:results];
 } else {
     // Log details of the failure
     NSLog(@"Error: %@ %@", error, [error userInfo]);
 }
}];

Please follow this link for detailed description and information:
Parse Compound Queries

Aamir
  • 16,329
  • 10
  • 59
  • 65