简体   繁体   中英

How do I force iCloud to sync data of an App on fresh install / first launch?

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.

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.

  1. In your app, check for the presence of a UserDefault with type Date named something like lastUpdated .
  2. If the value of lastUpdated is nil (which it will be on a fresh installation of a device) then do a CKQuery to fetch all records.
  3. After fetching the records, set lastUpdated to "now" using a new Date() object.
  4. You can use the lastUpdated date in the future to check for stale data. For example, if lastUpdated is more than n days old compared to now, you can pull the records again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM