You should use GCD
. It's clean and thread-safe.
dispatch_group_t group = dispatch_group_create(); //Create the group
[GKPlayer loadPlayersForIdentifiers:playerIds withCompletionHandler:^(NSArray *players, NSError *error)
{
for (GKPlayer *player in players)
{
dispatch_group_enter(group); //For each element, enter the group
[player loadPhotoForSize:GKPhotoSizeSmall withCompletionHandler:^(UIImage *photo, NSError *error)
{
dispatch_group_leave(group); //Leave group when the operation is done
}];
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//This is called when all process have leave the group
});
}];