我正在使用Game Center并加载所有个人资料图片:
- (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...
}];
}
}];
如何判断所有“loadPhotoForSize”块何时完成,并调用onComplete块?
提前致谢
答案 0 :(得分:3)
您应该使用GCD
。它干净且线程安全。
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
});
}];