swiftnsubiquitouskeyvaluestore

Syncing key values between devices


I'm trying to move from NSUserDefaults so that the values sync across devices. The value I'm trying to sync is a single bool, to identify if an initial data import has been done.

I'm using the following code

var keyValStore = NSUbiquitousKeyValueStore()

let firstRun: Bool = keyValStore.bool(forKey: "firstRun")
NSLog("First Run: \(firstRun)")
if(!firstRun) {
    keyValStore.set(true, forKey: "firstRun")
    keyValStore.synchronize()
}

If I run the app on my iPhone, it works as expected, the import only works once. If I then run the app on my iPad, it does that import again when it shouldn't, the log shows

First Run: false


Solution

  • Issue was resolved after deleting the app and rebooting devices