问题
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
UserDefault
with typeDate
named something likelastUpdated
. - If the value of
lastUpdated
isnil
(which it will be on a fresh installation of a device) then do aCKQuery
to fetch all records. - After fetching the records, set
lastUpdated
to "now" using a newDate()
object. - You can use the
lastUpdated
date in the future to check for stale data. For example, iflastUpdated
is more thann
days 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