Siavash Alp
Siavash Alp

Reputation: 1452

Remove an item from a column type of array in Parse (iOS SDK)

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

Upvotes: 3

Views: 3624

Answers (1)

James Frost
James Frost

Reputation: 6990

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];
}];

}

Upvotes: 9

Related Questions