如何强制iCloud在全新安装/首次启动时同步应用程序的数据? [英] How do I force iCloud to sync data of an App on fresh install / first launch?

查看:144
本文介绍了如何强制iCloud在全新安装/首次启动时同步应用程序的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在开发的游戏中,我已经实现了GKSavedGames,并且一切正常,但是如果卸载并重新安装游戏或将其安装在新设备上,则首次启动fetchSavedGamesWithCompletionHandler将返回0个已保存的游戏.

是否有一种方法可以强制iCloud将文件同步到设备?我已经尝试等待并重新调用fetchSavedGamesWithCompletionHandler,但是似乎没有任何效果.如果我立即重新启动该应用程序,则所有保存文件都可用.

解决方案

我对GKSavedGames并不熟悉,但是如果您将CloudKit用作帖子的标签,那么以下方法可能会对您有所帮助.

  1. 在您的应用中,检查是否存在类型为DateUserDefault,其名称类似于lastUpdated.
  2. 如果lastUpdated的值是nil(它将在全新安装的设备上),则执行CKQuery来获取所有记录.
  3. 在获取记录之后,将lastUpdated设置为"now".使用新的Date()对象.
  4. 您将来可以使用lastUpdated日期检查过时的数据.例如,如果lastUpdated与现在相比已超过n天,则可以再次提取记录.

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.

这篇关于如何强制iCloud在全新安装/首次启动时同步应用程序的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆