Alex Rivera | Logout

iCloud NSUbiquitousKeyValueStore initial sync/access delay - how to handle?

Asked 2012-09-21T23:11:45.320
20

I'm using NSUbiquitousKeyValueStore to store some app settings. My logic is: when I save data locally, I save it to NSUbiquitousKeyValueStore also as a backup. When I need settings, I read locally and I only use iCloud key-value store if no data is found locally (after app is reinstalled, for example). If user has several devices sharing one icloud id, he can write settings on one device and download them to another (I warn him about rewrite).

I have a strange issue. Steps:

  1. Installed an app and save its data to NSUbiquitousKeyValueStore. Made sure data is there.
  2. Removed the app (assuming data is still persists in iCloud).
  3. Waited several minutes just in case, then installed and launched the app from inside Xcode.
  4. Tried to read a settings key using [[NSUbiquitousKeyValueStore defaultStore] dataForKey: @"mykeyname"] - sometimes it's ok, but sometimes key is not found!
  5. Waited for 15 seconds, tried again. Success. Confused.

So it seems like ios needs some time to make remote key-value storage for my app available locally for dataForKey: call. If I'd wrote such a system (actually I did - some time ago, in another life) there obviously must be a delay before asking and receiving a key-value data. So I'd like to have some notification saying: "we finished downloading/syncing key-value storage on first start" or something similar.

As far as I understand I can work with NSUbiquitousKeyValueStore in main thread synchronously (which is convenient for me). But [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil] returns a valid url, and then I get "key isn't found". So I can't rely on it. Is there a way to be sure NSUbiquitousKeyValueStore works an is downloaded? It's important especially with slow internet.

UPDATE

Adding [[NSUbiquitousKeyValueStore defaultStore] synchronize] (as written in apple docs) to init and load was helped a littl

Edit
Report

1 Answer

4

Conclusion

Temporary solution is: - call synchronize before get data from key-value store - to be sure it would work "turn off iCloud->Documents&Data - turn off and again on network - turn on Documents&Data", but also you should wait several minutes before iCloud downloads all needed data

Note: when app is installed and already worked (saved/loaded) with key-value store updates of iCloud data are pretty fast (7-15 sec), but when you reinstall the app it seems that icloud needs several minutes to actualize key-value store.

I'd be glad to hear your thoughts, because icloud looks like almost unusable feature. But I don't want to set up my own server to merely get the same functionality.

answered 2012-09-22T00:05:44.760

Your Answer