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:
- Installed an app and save its data to NSUbiquitousKeyValueStore. Made sure data is there.
- Removed the app (assuming data is still persists in iCloud).
- Waited several minutes just in case, then installed and launched the app from inside Xcode.
- Tried to read a settings key using [[NSUbiquitousKeyValueStore defaultStore] dataForKey: @"mykeyname"] - sometimes it's ok, but sometimes key is not found!
- 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