sunshinejr / SwiftyUserDefaults

Modern Swift API for NSUserDefaults
http://radex.io/swift/nsuserdefaults/static
MIT License
4.85k stars 365 forks source link

iCloud sync support #228

Open StevenSorial opened 4 years ago

StevenSorial commented 4 years ago

Have you thought of adding ICloud support via NSUbiquitousKeyValueStore?

I think it's a very reasonable feature to include and would fit well with the Swifty part of this library given that the native API is not very intuitive.

if yes, would you be open to a PR? :)

sunshinejr commented 4 years ago

Hey @StevenMagdy, yes I did! This is something I would really like to have - if it's possible to have with how we structured the API nowadays. Would be really awesome if you'd like to open a PR 🙌

StevenSorial commented 4 years ago

Great :)

if it's possible to have with how we structured the API nowadays.

That's actually was going to be my next question 😄 - is it even possible or not? and what's your advice to implement such a feature?

sunshinejr commented 4 years ago

@StevenMagdy not sure as I never used the cloud-kit backend for User Defaults, but I quickly looked at the API and:

please remember that this is just pure speculation as I didn't touch the code but you can always ping me here or in the PR with questions and I'll try to help as soon as possible :)

StevenSorial commented 4 years ago

the global Defaults variable would use UserDefaults.standard as a provider as it is currently (and people that choose to use the cloud-kit backing could just override it)

@sunshinejr actually I was thinking of making it per key:

extension DefaultsKeys {
    var launchCount: DefaultsKey<Int> { return .init("launchCount", defaultValue: 0, synced: true) }
}

or better:

extension DefaultsKeys {
    var launchCount: DefaultsSyncedKey<Int> { return .init("launchCount", defaultValue: 0) }
}
sunshinejr commented 4 years ago

@StevenMagdy ah interesting! So do you have more use-cases when you store only a few defaults, rather than the whole suite?

StevenSorial commented 4 years ago

actually no, but I'm sure it will be needed by others because the whole synced container has a 1 MB limit, so some users will have to keep larger keys local.

MKiCloudSync uses the same approach but in a less static way

mman commented 4 years ago

I worked with both UserDefaults and UbiquitousKeyValueStore and they both are indeed quite the same and I have many times thought to open PR myself, so +1.

The only real difference is that iCloud defaults can change behind your back from another device, which is a pretty common case that needs to be handled nicely via API.

For example, with traditional UserDefaults, you start the app, read preferences and configure UI. With iCloud, you start the app, register observers, start the app, and fresh iCloud defaults modified on another device arrive several seconds later and you need to reconfigure the app.

This would require augmenting the existing API to add some kind of notification closures to the whole defaults suite or to each preference individually.

Just my .2 euro cents, Martin

sunshinejr commented 4 years ago

@mman ah this explains the code behind MKiCloudSync 😄 yeah, this seems a little bit tougher now!