Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for existing iCloud values before setting a value?

In my app, I need to share a setting between different devices running the app. I want the first device that install the app to set the master value of the setting, then all other devices should get that setting and not overwrite it.

  1. How do I make sure I first check if iCloud has a value before setting the value? So I don't overwrite an existing one.

  2. Should I wait for NSUbiquitousKeyValueStoreInitialSyncChange event to be sent, and then I can check for an eventual existing value and otherwise set it for the first time? If yes, can I rely on receiving the NSUbiquitousKeyValueStoreInitialSyncChange event? If not, then it might turn out that it don't set the iCloud value at all with this approach.

  3. If I try to set a value before NSUbiquitousKeyValueStoreInitialSyncChange is triggered for the first time, will it be discarded and then the NSUbiquitousKeyValueStoreInitialSyncChange will be triggered with the existing data in the store?

  4. I've heard NSUbiquitousKeyValueStoreInitialSyncChange is not triggered if there is no values in the store when it synces the first time?

I have read the Apple documentation about this and seen answers here on Stack Overflow, but don't understood how to do exactly this.

How can I make sure I don't overwrite an existing value the first time I launch/install the app?

like image 369
thejaz Avatar asked Aug 27 '12 11:08

thejaz


1 Answers

There is no way for you to surely know you have effectively synchronized with the distant store at least once and you should not count on it (imagine there is no iCloud account set up, or no connectivity or iCloud servers are down, etc.: you don't want your user to wait for you to be sure you are in sync with the cloud as it can take forever or even never happen).

What you should do:

  • when you start, check the store to see if there is a value.
  • If there is no value, just push your own value.
  • If the initial sync with the server did not happen yet and there is, in fact, a value in the cloud, this will be considered a conflict by the NSUbiquitousKeyValueStore. In this precise case (initial sync), the automatic policy is to revert your local value and prefer the one in the cloud instead. So your application will be notified by the NSUbiquitousKeyValueStoreDidChangeExternallyNotification of this revert with the NSUbiquitousKeyValueStoreInitialSyncChange reason.
  • If there was in fact no value in the cloud, your local value will be pushed and everyone will be happy.
like image 56
Julien Avatar answered Nov 20 '22 16:11

Julien