I'm having nothing but trouble with NSUbiquitousKeyValueStore and getting it to reliably sync across two devices. It's starting to drive me mad. Mad.
I watch my code make a call to:
[[NSUbiquitousKeyValueStore defaultStore] setObject:_someData forKey:@"SomeData"];
[[NSUbiquitousKeyValueStore defaultStore] synchronize];
But more times than not, the data never shows up on the other device.
I've seen it sync within a few seconds and other times I've waited for an hour and never seen a sync. I plug the devices into xcode and start debugging and sometimes the data suddenly pops up and other times it doesn't. Sometimes it does.
After making this call, quit and restarting the App, I see the old data if I read it, not the new data I supposedly just posted. On the same device.
When the App starts up, I make a call to:
_someData = [[NSUbiquitousKeyValueStore defaultStore] stringForKey:@"SomeData"];
I'm assuming there is no init code that needs to be run. No example I've found does.
When it works, it works quite well.
Then tonight I was looking at the device logs and found this right after my last exit (and attempt to send data):
Jul 8 21:32:03 unknown syncdefaultsd[17296] <Warning>: Error writing storage for com.mycompany.myapp to /private/var/mobile/Applications/4AC8C56E-6060-408B-84F9-F7EC336221D9/Library/SyncedPreferences/com.mycompany.myapp.plist: Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0xde80d40 {NSFilePath=/private/var/mobile/Applications/4AC8C56E-6060-408B-84F9-F7EC336221D9/Library/SyncedPreferences, NSUnderlyingError=0xde80b20"The operation couldn’t becompleted. No such file or directory"}
That path is not the valid path from my Apps sandbox (wrong GUID), so I wonder if this is the root of my problems. I do delete and re-install the App a lot during development, so I wonder if there is some old path cached somewhere and it's trying to sync that data. Is there a way to clear it is?
I know all my entitlements are set up correctly since it does work sometimes.
[[NSUbiquitousKeyValueStore defaultStore] synchronize]
is returning YES
.
Has anyone else seen this problem or that error? Is there something else I need to be doing when my App starts up and when I send data to the cloud? According to the docs and examples, it seems pretty easy.