How to test iCloud synchronisation between Cocoa app and iOS app

470 views Asked by At

My problem is simple:

I'm now developing Cocoa application which stores data in iCloud (key value store). In order to test iCloud synchronisation I've build a simple iOS app, and installed it in my device.

I did iCloud initialisation in cocoa app:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataUpdatedFromCloud:) name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification object:[NSUbiquitousKeyValueStore defaultStore]];
[[NSUbiquitousKeyValueStore defaultStore] synchronize];

and selector:

+(void)dataUpdatedFromCloud:(NSNotification *)notification {
    NSLog(@"dataUpdatedFromCloud: %@", [[NSUbiquitousKeyValueStore defaultStore] objectForKey:@"theKey"]);
}

in order to catch data when it comes from iCloud.

I do everything same in iOS app, with extra action when I store data in NSUbiquitousKeyValueStore:

// Script 1
[[NSUbiquitousKeyValueStore defaultStore] setDictionary:@{@"id":@1} forKey:@"theKey"];

My testing scenario is:

  1. Run cocoa application in Xcode
  2. Run iOS app in my device
  3. In iOS app I have a button, which calls Script 1
  4. Here I expect dataUpdatedFromCloud is called in 10-20 seconds on Cocoa app, but it doesn't!!!

Am I doing everything correct, or maybe I understood something incorrectly?

2

There are 2 answers

0
Tom Harrington On

Well, probably, but reasons it might fail include:

  • The iCloud container ID is not exactly the same on the two apps. On the one hand this is obvious, but on the other it's the easiest thing to screw up, so check again.
  • The iCloud container ID is OK but the provisioning profile doesn't actually enable iCloud in both apps. You should check ubiquityIdentityToken in both apps to verify this.
  • The value has already been synced, for example, when the app wasn't running. The notification you're looking for only gets posted if the change happened while the app was running and if the new value is different than the old one. Since you're always setting @1 as the value, it's entirely possible that the value has already been received and that new attempts aren't changing anything. For testing, it might help to replace @1 with something like @([NSDate timeIntervalSinceReferenceDate]) just to make sure you get a different value every time.
  • iCloud is temporarily confused/down/broken/just plain slow. Although 10-20 seconds is reasonable, there's no performance guarantee. If it shows up after 30 seconds, or even 30 minutes, it's still working as advertised.
0
Steven Hovater On

I've had some success with gummed up iCloud sync by resetting iCloud's documents and data as described here: http://support.apple.com/kb/HT5824

Sometimes iCloud gets confused about what it has already synced and where—testing, particularly when you're making repetitive identical updates, makes that more likely in my experience.