问题
In a game I am working on I have implemented GKSavedGames and everything is working correctly except that if the game is uninstalled and reinstalled or installed on a new device the first initial launch fetchSavedGamesWithCompletionHandler returns 0 saved games.
Is there a way to force iCloud to sync the files to the device? I have tried waiting and recalling fetchSavedGamesWithCompletionHandler but nothing seems to work. If I immediately relaunch the app all the save files are then available.
回答1:
I'm not familiar with GKSavedGames, but if you are using CloudKit as your post's tag implies, then the following approach may help you.
- In your app, check for the presence of a
UserDefaultwith typeDatenamed something likelastUpdated. - If the value of
lastUpdatedisnil(which it will be on a fresh installation of a device) then do aCKQueryto fetch all records. - After fetching the records, set
lastUpdatedto "now" using a newDate()object. - You can use the
lastUpdateddate in the future to check for stale data. For example, iflastUpdatedis more thanndays old compared to now, you can pull the records again.
来源:https://stackoverflow.com/questions/64021778/how-do-i-force-icloud-to-sync-data-of-an-app-on-fresh-install-first-launch