1

I have been using iCloud in my apps for a while now and am recently having an issue where devices refuse to talk to each other. Or at least that's what I thought until I started logging the methods where the merging takes place. My persistentStoreCoordinator is set up as described in a previous question of mine.

The problem is the following. When setting up my managedObjectContext, I add an observer to it to view the NSPersistentStoreDidImportUbiquitousContentChangesNotification notification like follows:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeChangesFrom_iCloud:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:coordinator];

where coordinator is the set up as NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator].

My iCloud methods are as follows:

- (void)mergeiCloudChanges:(NSNotification *)note forContext:(NSManagedObjectContext *)moc {
    NSLog(@"insert %@", [[note userInfo] valueForKey:@"inserted"]);
    NSLog(@"delete %@", [[note userInfo] valueForKey:@"deleted"]);
    NSLog(@"update %@", [[note userInfo] valueForKey:@"updated"]);
    [moc mergeChangesFromContextDidSaveNotification:note];

    NSNotification *refreshNotification = [NSNotification notificationWithName:@"RefreshAllViews" object:self userInfo:[note userInfo]];
    [[NSNotificationCenter defaultCenter] postNotification:refreshNotification];
}

- (void)mergeChangesFrom_iCloud:(NSNotification *)notification {
    NSLog(@"merging changes");
    NSManagedObjectContext *moc = [self managedObjectContext];
    [moc performBlock:^{
        [self mergeiCloudChanges:notification forContext:moc];
    }];
}

So now we have the actual problem

At first, syncing went flawlessly. Data from one device was changed, then that change appeared on the other device. But now, nothing. The curious part is in those logs you see in mergeiCloudChanges:forContext:. The merge actually occurs. I see this triggered. But the insert, delete, and update parts of the merge note are ALWAYS without content. So the merge occurs, but with no data. For the life of me, I cannot figure out why this is or how to get it to sync properly again. Any help would be more than appreciated.

Note: I am also using the NSUbiquitousKeyValueStoreDidChangeExternallyNotification notification for my NSUbiquitousKeyValueStore (which I use to sync the NSUserDefault key-values), and those notifications transfer over and update without any hiccups at all, which confuses me even more as to why the persistentStoreCoordinator notifications are blank.

I truly hope someone has seen/experienced this before because after two weeks exhausting every avenue I can think of to find and fix this on my own, I feel I am now at a standstill.

Community
  • 1
  • 1
justin
  • 5,811
  • 3
  • 29
  • 32
  • do you see any iCloud errors in the log? Have you tried to nuke all your app iCloud data, delete the app from the devices and install it clean? – Kostiantyn Sokolinskyi Feb 20 '12 at 17:03
  • @KostiantynSokolinskyi I haven't seen any errors in the log, no. Every now and then, there's a "couldn't download item" error, but those are quickly resolved as iCloud retries the download itself. I have done fresh installs, and removed the iCloud directory (using `NSFileManager` to remove the cloudURL where the transaction logs are held). No luck. At this point, I can get one device to talk to the other, but not the other way around. For the life of me, I can't figure out what's going wrong. – justin Feb 21 '12 at 01:27
  • Isn't mergeChangesFromContextDidSaveNotification to transfer changes between threads (at least in the old approach)? But NSPersistentStoreDidImportUbiquitousContentChangesNotification informs you about changes from the iCloud. Is a mergeChangesFromContextDidSaveNotification really required? In my test, changes from iCloud are already imported after this notification. – Stephan Mar 22 '12 at 20:30
  • Hey @StephanZehrer, the `mergeChangesFromContextDidSaveNotification` may or may not be necessary. I just grabbed that out of the iCloud version of the Apple Recipes code. If you are noticing the merge process happens regardless, I may try to remove that from the code. I have since found a fix to this problem, but since it was drastic (removing the iCloud data completely and starting from scratch to get a fully functioning sync), I haven't set an answer as I'm still working for a less invasive approach – justin Mar 27 '12 at 19:55
  • @slev I didn't had a problem with the notifications. I just used the code in seem working. I just try to understand what it does or why I need it. But the WWDC 2011 Session 303 explain it. I got in some conditions this error: http://stackoverflow.com/questions/8937805/how-can-i-resolve-this-icloud-error – Stephan Mar 29 '12 at 05:33
  • @StephanZehrer that error would explain a lot. Having the store not present to successfully sync the features. The weirdest thing for me was that the key-value user defaults would sync on both continuously while the Core Data items would refuse to talk to each other. But again, that error you linked would explain it. Now it's just a matter of making it go away – justin Mar 30 '12 at 02:39
  • @slev well i had some problems after i deleted the ubiquitous container as defined in documentation. I still in testing some use-cases as removing the app and reinstall, install a new instance on a further device and so on. In this thread they discuss the sync problem: https://devforums.apple.com/thread/126670?start=425&tstart=0 – Stephan Mar 30 '12 at 05:26

1 Answers1

0

I've got this one figured out. Basically, the transaction logs appeared to be corrupted or misplaced. I have reworked a way to resycn devices and will be posting the results immediately on a previous question of mine

Community
  • 1
  • 1
justin
  • 5,811
  • 3
  • 29
  • 32