Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iCloud KeyValue store not recognized on first launch

My app uses iCloud (key-value store) to sync an unique id between multiple devices. This works accept at the point were it really has to work, at the first launch of the app. It looks like the device isn't yet familiar with the values from iCloud at first launch, only after the app is installed and has been running for a while.

I check for the iCloud value in the viewDidLoad function on main view of the app.

So, my questions:

  • Is this the expected behavior?
  • If yes, is there another solution?
  • Could it be the case this is only a problem when running from Xcode, not the shipping version? If so, how to test?

Thanks!

Jasper

like image 232
Jasper Bel Avatar asked Jul 16 '12 16:07

Jasper Bel


2 Answers

I had a similar issue where the NSUbiquitousKeyValueStoreDidChangeExternallyNotification wasn't triggering on the first launch after install, no matter how long I waited. Setting an initial key in the NSUBiquitousKeyValueStore seemed to solve this.

Immediately after adding the observer to the default store, I call:

[[NSUbiquitousKeyValueStore defaultStore] setString:@"testValue" forKey:@"testKey"];
[[NSUbiquitousKeyValueStore defaultStore] synchronize];

I use different keys (i.e. not testKey) for the actual data I want to sync.

like image 128
Jellyjoey Avatar answered Oct 01 '22 15:10

Jellyjoey


When you first run an iCloud enabled App it has to pull all the data from Apple servers. The time it takes to do so depends on many things like what kind of network you're currently on (Edge, 3G, GPRS, WLAN). It also depends on how much traffic the iCloud server currently has to handle so the request to the iCloud server may take a few more seconds, no matter what type of network connectivity you have.

To sum it up: Yes, it sounds absolutely normal.

If running your App depends on those settings consider implementing a "Wait" or "Loading" view that stays on the screen as long as it takes for the App to perform a initial synch and load all needed data from the cloud. To not block the UI forever also implement a timeout for this view (if iCloud data not loaded within X seconds, dismiss the view and notify user).

like image 43
Björn Kaiser Avatar answered Oct 01 '22 13:10

Björn Kaiser