如何强制iCloud在全新安装/首次启动时同步应用程序的数据? [英] How do I force iCloud to sync data of an App on fresh install / first launch?
问题描述
在我正在开发的游戏中,我已经实现了GKSavedGames,并且一切正常,但是如果卸载并重新安装游戏或将其安装在新设备上,则首次启动fetchSavedGamesWithCompletionHandler
将返回0个已保存的游戏.
是否有一种方法可以强制iCloud将文件同步到设备?我已经尝试等待并重新调用fetchSavedGamesWithCompletionHandler
,但是似乎没有任何效果.如果我立即重新启动该应用程序,则所有保存文件都可用.
我对GKSavedGames
并不熟悉,但是如果您将CloudKit用作帖子的标签,那么以下方法可能会对您有所帮助.
- 在您的应用中,检查是否存在类型为
Date
的UserDefault
,其名称类似于lastUpdated
. - 如果
lastUpdated
的值是nil
(它将在全新安装的设备上),则执行CKQuery
来获取所有记录. - 在获取记录之后,将
lastUpdated
设置为"now".使用新的Date()
对象. - 您将来可以使用
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.
- 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.
这篇关于如何强制iCloud在全新安装/首次启动时同步应用程序的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!