我有以下代码将应用程序的数据推送到云端并使用 iCloud 值更新设备存储。
但是,它不会使用 iCloud 值更新设备存储。
要同步的两个最重要的值是硬币和积分,我将两者都包含在下面的代码中,以防出现问题。
更新 iCloud:
func updateiCloud(){
if (Reachability.isConnectedToNetwork()){
if (defaults.bool(forKey: "UpdatedLocal")){
iCloudKeyStore?.set(Int64(points), forKey: "Points")
iCloudKeyStore?.set(Int64(coins), forKey: "Coins")
iCloudKeyStore?.synchronize()
//other values are synced
}
}
}
更新本地:
func updateLocal() {
if (Reachability.isConnectedToNetwork()){
if (!defaults.bool(forKey: "UpdatedLocal")) {
defaults.set(iCloudKeyStore?.longLong(forKey: "Points"), forKey: "Points")
points = defaults.integer(forKey: "Points")
defaults.set(iCloudKeyStore?.longLong(forKey: "Coins"), forKey: "Coins")
coins = defaults.integer(forKey: "Coins")
defaults.set(true, forKey: "UpdatedLocal")
}
else {
defaults.set(false, forKey: "firstLaunch")
AppDelegate.firstLaunch = false
defaults.set(true, forKey: "UpdatedLocal")
}
}
else {
defaults.set(false, forKey: "UpdatedLocal")
}
}