Search code examples
iosobjective-cparse-platformnsmutablearray

Why is my NSMutable not being populated correctly when inserting objects from another NSArray?


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:

  1. Create a PFQuery object
  2. Query the Participants Class for an specific ObjectId
  3. If no error, then we create a NSMutable array
  4. If no participants are in Parse then we insert the current user as participant.
  5. Else, insert all participants in the mutable array and add currentuser at the end of the array.
  6. Then upload it again to Parse

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.


Solution

  • 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.