У меня есть PFQuery
, который получает текущих участников определенного события:
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]);
}
}];
Вот как работает метод:
- Создание объекта
PFQuery
- Запросить класс участников для определенного
ObjectId
- Если ошибок нет, мы создаем
NSMutable array
- Если в Parse нет участников, то мы вставляем текущийпользователь как участник.
- Иначе, вставьте всех участников в изменяемый массив и добавьте
currentuser
в конец массива. - Затем снова загрузите его в Parse
Моя проблема в шаге 5:
Когда я выполняю задачи в другом, столбец в Parse выглядит следующим образом:
[["Участник 1 "]," Участник 2 "]
Но мне бы хотелось, чтобы это было так:
[" Участник 1 "," Участник 2 "]
То, что я пробовал:
Я пробовал такие вещи, как размещение массивов следующим образом.[newParticipantsArray addObject:[[objects objectAtIndex:i] valueForKey:@"Participants"]];
и подобные комбинации, конечно без удачи.