Reputation: 173
I can save my game and also fetch it but if I uninstall the game and then install it again, the call to fetchSavedGamesWithCompletionHandler
returns 0 games (even if I call it several times). And if I call it again after some few seconds, the GKLocalPlayer manages to fetch the game i previously saved.
The error is nil so there is no problem with connecting to Game Center or iCloud (if there was problem with iCloud, the error wouldn't be nil and it would say that the user isn't connected to iCloud or that iCloud Drive isn't on).
This is not the same if anyone thought of sharing that with me: https://stackoverflow.com/questions/34445258/gklocalplayer-fetchsavedgameswithcompletionhandler-result-depends-on-device
Sure I can just fix it by implementing a timer and then call fetchSavedGamesWithCompletionHandler
accordingly, but that's just a bad way of fixing it considering that the game is gonna support the slower devices (iPhone 5s compared to iPhone 7).
Upvotes: 11
Views: 778
Reputation: 91
This is my workaround using sleep.
I call fetchSavedGamesWithCompletionHandler from inside the authenticateHandler using dispatch_async like below.
As for me, sleep for 1.7 or more seconds worked with iPad mini 5th. Sleep for 4.1 or more seconds worked with iPhone5s. Please change it as you like.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//[NSThread sleepForTimeInterval:1.7];//minimum for iPad mini 5th
//[NSThread sleepForTimeInterval:4.1];//minimum for iPhone5s
[NSThread sleepForTimeInterval:5.0];
[localPlayer fetchSavedGamesWithCompletionHandler:^(NSArray<GKSavedGame *> * _Nullable savedGames, NSError * _Nullable error) {
//do something...
}];
}
2023 Feb 3rd: add and modify
I found that returning 0 games can occur not only on installation.
It occurs,
So, we must wait whenever 0 games are returned.
To handle this, if 0 games are returned, retry several times with a few seconds delay.
Required retry count depends on the device specs and other processes running in the App.
To me, iPhone5s - the lowest spec device I have - needs 4 retry with 3 seconds delay.
Upvotes: 1