Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUbiquitousKeyValueStore vs NSUserDefaults

From Apple's NSUbiquitousKeyValueStore documentation:

If you write to the key-value store object when the user is not signed into an iCloud account, the data is stored locally until the next synchronization opportunity. When the user signs into an iCloud account, the system automatically reconciles your local, on-disk keys and values with those on the iCloud server.

Therefore if a user never signs into an iCloud account, the key-value store object is stored locally indefinitely, much like NSUserDefaults.

In this case, should we all stop using NSUserDefaults and just use NSUbiquitousKeyValueStore as a 'default' for all apps? What are the disadvantages of this approach?

An advantage I can see is that from a user perspective the app preferences will be synced across all their devices, which is most likely a better user experience!

like image 744
David T Avatar asked Apr 25 '17 20:04

David T


People also ask

What is Nsubiquitouskeyvaluestore?

An iCloud-based container of key-value pairs you use to share data among instances of your app running on a user's connected devices.

What is NSUserDefaults?

The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an app to customize its behavior to match a user's preferences. For example, you can allow users to specify their preferred units of measurement or media playback speed.


2 Answers

We should clearly understand that NSUbiquitousKeyValueStore in the iCloud is for the configuration and tracking of the app state across all devices of the certain account.

Some facts bout NSUbiquitousKeyValueStore.

In the documentation we can find that :

Avoid using this class for data that is essential to your app’s behavior when offline; instead, store such data directly into the local user defaults database.

Also size of the data that is possible to save is relatively small.

The total amount of space available in your app’s key-value store, for a given user, is 1 MB. There is a per-key value size limit of 1 MB, and a maximum of 1024 keys.

like image 57
Oleg Gordiichuk Avatar answered Sep 28 '22 01:09

Oleg Gordiichuk


I have found if the user goes into their iCloud settings and turns your app switch off - you will no longer be able to save to NSUbiquitousKeyValueStore.

Your app will start with it enabled. Then you may save data there. Once iCloud is turned off, it will always be accessible to read - but if you update the data and restart the app, i believe you will see it revert back to what it was before the user turned off iCloud support in iCloud settings.

This is a problem I am working with at the moment. I am trying to find a way to test if that switch is on/off and change my data store to NSUserDefaults.

Correct me if I am wrong but that is what I have discovered so far.

-mark

edit: this is only if you have registered for iCloud Documents. If you are ONLY using NSUbiquitousKeyValueStore it should be virtually the same - I believe.

edit-edit: now its acting different. The switch is there when ONLY NSUbiquitousKeyValueStore is checked in the iCloud capabilities. So looking - again - for a way to detect if that switch is flipped for an app in iCloud Settings.

like image 39
Mark A. Avatar answered Sep 28 '22 03:09

Mark A.