2012年6月25日月曜日

iCloud key-value store



Want to use iCloud's key-value store, NSUbiquitousKeyValueStore, to synchronize data in your app between devices?
 
I've started using NSUbiquitousKeyValueStore in my game "Harigana" but noticed a flaw that I would like to share. Nothing much really, just a small tip.

The recommendation is that you early in your app start cycle call

[[NSUbiquitousKeyValueStore defaultStore] synchronize];

and then start listen to the notification NSUbiquitousKeyValueStoreDidChangeExternallyNotification.

This will call your app when the store is changed on other devices. However, this seems to be reliable only if the app is active at the time the other device changes a value. If the app is not running at the time the store is updated you might not get notification when you wake the app up. Sometimes it works, sometimes not, according to my experience.

Therefor, to be sure you have the latest value from iCloud, force a synchronization at app startup/wakeup. Simply iterate through all values in your NSUbiquitousKeyValueStore and overwrite the local value with the value from the store. Of course, this requires that when you store the values, always store the them in both iCloud's key-value store and the user defaults, otherwise you might overwrite users hard earned points with something that could be zero. iOS will take care of everything for you under the hood, whether the user has activated iCloud or not.

Remember, the truth is in the Cloud.
140 180 iOS

記載されている会社名、および商品名等は、各社の商標または登録商標です。

0 コメント:

コメントを投稿

Related Posts Plugin for WordPress, Blogger...