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

隐身守侯 提交于 2020-12-15 06:03:51

问题


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.

  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.


来源:https://stackoverflow.com/questions/64021778/how-do-i-force-icloud-to-sync-data-of-an-app-on-fresh-install-first-launch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!