Question

I think I'm overcomplicating this scenario. I'm working through a join table in a many to many situation.

If I find a join, I want to delete it, if I don't find one, I want to add it.

Adding a new join works fine. I can't figure how to delete ..

This is my code. If anyone sees anything wrong - or a better way to do this, please advise.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%s", __FUNCTION__);

    //Create query for all current user objects
    PFQuery *query = [PFQuery queryWithClassName:@"DDPeopleJoin"];
    [query whereKey:@"parseUser" equalTo:[PFUser currentUser]];

    [query whereKey:@"serviceKey" equalTo:[currentService valueForKey:@"serviceKey"]];
    [query whereKey:@"personKey" equalTo:[currentPerson valueForKey:@"personKey"]];

    query.cachePolicy = kPFCachePolicyCacheThenNetwork;

    // Run the query  -  if there is an object delete it otherwise, go to JoinPeople
    allDeadPeople = [NSMutableArray new];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            if (!objects) {
                NSLog(@"New Person");
                [self joinPeople];
                return;
            }
            NSLog(@"Found a match, erase them");

            for (PFObject *object in objects) {
                [object deleteInBackground];
            }
            [self refreshJoins:self];
        }

    }];

}
Was it helpful?

Solution

Firstly, instead of using enumerate block to delete each object, you can use the following:

 [PFObject deleteAllInBackground:objects];

Secondly, You might get trouble with [self refreshJoins:self coz it will run before all object can be deleted. You should put that in the -deleteInBackground:block to make sure [self refreshJoins:self]` works properly

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top