I have a PFQuery
that gets the current participants of a particular event:
PFQuery *getcurrentparticipants = [PFQuery queryWithClassName:@"Event"];
[getcurrentparticipants selectKeys:@[@"Participants"]];
[getcurrentparticipants whereKey:@"objectId" equalTo:ObjectID];
[getcurrentparticipants findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSMutableArray *newParticipantsArray = [[NSMutableArray alloc]init];
if([objects[0] valueForKey:@"Participants"] == nil){ // If object retrieved in objects is null. If there are 0 participants
[newParticipantsArray addObject:PFUser.currentUser.username];
PFQuery *query = [PFQuery queryWithClassName:@"Event"];
[query getObjectInBackgroundWithId:self.ObjectID
block:^(PFObject *Event, NSError *error) {
Event[@"Participants"] = newParticipantsArray;
[Event incrementKey:@"Vacants" byAmount:[NSNumber numberWithInt:-1]];
[Event saveInBackground];
}];
}else{ // STEP 5
for(int i=0;i<objects.count;i++) {
[newParticipantsArray addObject:[[objects objectAtIndex:i] valueForKey:@"Participants"]];
}
[newParticipantsArray addObject:PFUser.currentUser.username];
NSLog(@"Part to upload %@", newParticipantsArray);
PFQuery *query = [PFQuery queryWithClassName:@"Event"];
[query getObjectInBackgroundWithId:self.ObjectID
block:^(PFObject *Event, NSError *error) {
Event[@"Participants"] = newParticipantsArray;
[Event incrementKey:@"Vacants" byAmount:[NSNumber numberWithInt:-1]];
[Event saveInBackground];
}];
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
This is how the method works:
PFQuery
objectObjectId
NSMutable array
currentuser
at the end of the array.My problem is in step 5:
When I perform the tasks in the else, the column in Parse looks like this :
[["Participant 1"],"Participant 2"]
But I would like to have it like this:
["Participant 1","Participant 2"]
What I have tried:
I tried things like putting the arrays like this. [newParticipantsArray addObject:[[objects objectAtIndex:i] valueForKey:@"Participants"]];
and similar combinations, of course without luck.
If in fact the value returned for the "Participants" key is an array, you can add the objects in it to your mutable array by doing the following:
NSArray* participants = [[objects objectAtIndex:i] valueForKey:@"Participants"]
[newParticipantsArray addObjectsInArray:participants];
This uses the addObjectsInArray:
method of NSMutableArray
to add the objects from the old array into the new one.