3

I have table name "Events". In that table I have a column of type array of string. I'm struggling with how I can delete only one element from that column. Consider the image below, I want to delete all the occurrences of "iYYeR2a2rU" from the "usersIncluded" column, without deleting the rows. I've used the removeObject:(id) forKey:(NSString *) and it didn't work.

image

This is how I'm trying to achieve it:

  PFQuery *query = [PFQuery queryWithClassName:@"Events"];
    NSArray *eventObjects = [query findObjects];
    [query whereKey:@"usersIncluded" equalTo:[self.uniqeFriendList objectAtIndex:indexPath.row]];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
        for (int i = 0; i<objects.count; i++) {
            PFObject *event = [eventObjects objectAtIndex:i];
            [event removeObject:[self.uniqeFriendList objectAtIndex:indexPath.row] forKey:@"usersIncluded"];
        }
    }];
}

The self.uniqeFriendList is a mutable array containing the ids that I want to delete from the 'usersIncluded' column.

Thanks in Advance

2
  • Do you have a PFObject subclass in your code for Event? Commented Jan 23, 2014 at 7:27
  • @JamesFrost No I don't have. Commented Jan 23, 2014 at 7:42

1 Answer 1

9

I think you're using the right method (removeObject:forKey: should do exactly what you want) but I think you're working with objects from the wrong array. You're performing your query twice, and within the findObjectsInBackgroundWithBlock: you're working with the array from the first time you called it... Try this:

PFQuery *query = [PFQuery queryWithClassName:@"Events"];
[query whereKey:@"usersIncluded" equalTo:[self.uniqeFriendList objectAtIndex:indexPath.row]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
    for (int i = 0; i <objects.count; i++) {
        PFObject *event = [objects objectAtIndex:i];    // note using 'objects', not 'eventObjects'
        [event removeObject:[self.uniqeFriendList objectAtIndex:indexPath.row] forKey:@"usersIncluded"];
    }

    [PFObject saveAll:objects];
}];

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.