Expect existing iCloud values ​​before setting a value?

In my application, I need to share the setting between the various devices using the application. I want the first device that installed the application to set the main value of the parameter, then all other devices should receive this parameter and not overwrite it.

  • How do I make sure that I check first if iCloud has a value before setting the value? Therefore, I do not overwrite the existing one.

  • Should I wait for NSUbiquitousKeyValueStoreInitialSyncChange to be sent, and then I can check for a possible existing value and otherwise set it for the first time? If so, can I expect to receive an NSUbiquitousKeyValueStoreInitialSyncChange event? If not, it may turn out that it does not set the iCloud value at all with this approach.

  • If I try to set the value before NSUbiquitousKeyValueStoreInitialSyncChange is started for the first time, will it be canceled and then NSUbiquitousKeyValueStoreInitialSyncChange will be started with existing data in the repository?

  • I heard that NSUbiquitousKeyValueStoreInitialSyncChange does not start if there are no values ​​in the repository when they are synchronized for the first time?

I read the Apple documentation about this and saw the answers here in the Stack Overflow section, but I don’t understand how to do this.

How can I make sure that I do not overwrite the existing value when I first start / install the application?

+8
ios objective-c ios5 icloud nsubiquitouskeyvaluestore
source share
1 answer

You cannot know for sure that you synchronized effectively with the remote storage at least once, and you should not count on it (imagine that there is no iCloud account, or there are no connections or iCloud servers, etc ..: you do not you want your user to wait for you to make sure that you are in sync with the cloud, as this may forever or even never happen).

What you should do:

  • at startup, check the store to see if there is a value.
  • If there is no value, just click your own value.
  • If the initial synchronization with the server has not yet occurred, and in fact this value is in the cloud, this will be considered a conflict using NSUbiquitousKeyValueStore . In this particular case (initial synchronization), the automatic policy is to return your local value and instead select the one that is in the cloud. Thus, your application will be notified by NSUbiquitousKeyValueStoreDidChangeExternallyNotification this return with the argument NSUbiquitousKeyValueStoreInitialSyncChange .
  • If there was no value in the cloud, your local value will be pressed and everyone will be happy.
+8
source share

All Articles