Вопрос

I'm using Game Center and loading all profile images like this:

- (void)loadProfileImages:(void(^)())onComplete

     [GKPlayer loadPlayersForIdentifiers:playerIds withCompletionHandler:^(NSArray *players, NSError *error)
      {
          for (GKPlayer *player in players)
          {
              [player loadPhotoForSize:GKPhotoSizeSmall withCompletionHandler:^(UIImage *photo, NSError *error)
               {
                   // Do something...
               }];
          }
      }];

How can I tell when all the "loadPhotoForSize" block has finished, and call the onComplete block?

Thanks in advance

Это было полезно?

Решение

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

    }];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top