アプリのデータをクラウドにプッシュし、iCloud の値を使用してデバイスのストレージを更新する次のコードがあります。
ただし、デバイス上のストレージは iCloud の値で更新されません。
同期する最も重要な 2 つの値はコインとポイントです。問題が発生した場合に備えて、以下のコードに両方を含めています。
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")
}
}