59
votes

So, it finally happened. The worst case scenario for any independent iPhone developer occurred. Several users are reporting complete data loss after upgrading my app. iCloud Core Data sync is not working. My users are using this app partially to run their businesses. This is a truly catastrophic failure.

The only iCloud related thing I changed was to add the key-value store to iCloud. The core data code remained exactly the same, same model version (no migration) etc.

In my tests everything worked beautifully! But to my dismay, users reported that their data was not there anymore when they opened the updated app.

What could be the reason for this?

  • The persistent store URL (an ubiquitous URL) should not have changed.
  • Merge conflicts are also unlikely, as this problem should have arisen before the update.
  • Some interference with the new ubiquitous key-value store perhaps?
    (I have pretty much ruled this out.)

Below please find the code for my managed object model and persistent store. Let me know if you need anything else to assess the problem.

- (NSManagedObjectContext *)managedObjectContext {

    if (managedObjectContext_ != nil) {
        return managedObjectContext_;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext_ = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        [managedObjectContext_ performBlockAndWait:^{
            [managedObjectContext_ setPersistentStoreCoordinator:coordinator];
            if (useICloud) {
                [managedObjectContext_ setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
                [[NSNotificationCenter defaultCenter] addObserver:self
           selector:@selector(mergeiCloud:)
           name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
           object:coordinator];
            }
        }];
    }
    return managedObjectContext_;
}

and

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (persistentStoreCoordinator_ != nil) {
        return persistentStoreCoordinator_;
    }

    NSMutableDictionary *options = [NSMutableDictionary dictionary];
    NSURL *storeURL = [[self applicationDocumentsDirectory] 
            URLByAppendingPathComponent:@"SalesCalls.sqlite"];

    [options setObject:[NSNumber numberWithBool:YES] 
                  forKey:NSMigratePersistentStoresAutomaticallyOption];
    [options setObject:[NSNumber numberWithBool:YES] 
                  forKey:NSInferMappingModelAutomaticallyOption];

    if (useICloud) {  // this is an internal flag set to YES
        NSURL *iCloudURL = [[NSFileManager defaultManager] 
                               URLForUbiquityContainerIdentifier:nil];

        if (nil!=iCloudURL) {
            NSString *cloudData = [[iCloudURL path] 
                       stringByAppendingPathComponent:@"data"];
            iCloudURL = [NSURL fileURLWithPath:cloudData];      

            [options setObject:@"at.topofmind.salesplus.store" 
                        forKey:NSPersistentStoreUbiquitousContentNameKey];
            [options setObject:iCloudURL 
                        forKey:NSPersistentStoreUbiquitousContentURLKey];

            NSURL *nosyncDir = [iCloudURL 
                        URLByAppendingPathComponent:@"data.nosync"];
            [[NSFileManager defaultManager] createDirectoryAtURL:nosyncDir 
                        withIntermediateDirectories:YES 
                        attributes:nil error:nil];

            storeURL = [nosyncDir 
                        URLByAppendingPathComponent:@"SalesCalls.sqlite"];
        }
    }

    NSError *error = nil;
    persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] 
             initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator_ 
                  addPersistentStoreWithType:NSSQLiteStoreType
                  configuration:nil URL:storeURL options:options error:&error]) 
   {
        NSLog(@"Cannot create persistent store coordinator, %@, %@", 
                        error, [error userInfo]);
        abort();
    }    

    return persistentStoreCoordinator_;
}

Comments, opinions, wild guesses etc. (and of course solutions!) are all welcome.

UPDATE:

One of my customers lost all his data and after reinstalling and resetting everything (rebooting device, etc.) he could not sync with iCloud any more. The symbol simply does not show up in the Settings.

1
Wild guess . Are you 100% sure you are attaching to the correct ubiquity container in the iCloud setup?Warren Burton
Hi Mundi, just want to say that it is entirely possible this is just buggyness on Apple's behalf. Their cloud stuff has come under flak in the past and my understanding is that their iCloud iOS stuff is quite flaky. tuaw.com/2012/11/27/…occulus
@WarrenBurton - Yes, see the code above. It's all there. I removed the log statements, but I did check the ubiquity URL. BTW, I would not know what would make them "correct" or not, as long as they remain the same.Mundi
@occulus Tell me about it... That's why I put "More ... woes" into the title.Mundi
Do yourself a favor and give up on iCloud and Core Data if you can. It's simply not ready, and I have never heard of any developer who got the combination to work reliably. If you find one (ideally with open source code), please point them out. (Even in apps where it supposedly works, you can be pretty sure that syncing simply stops after a while.)mrueg

1 Answers

1
votes

I was facing something similar with few uses ( by your description I assumed you have way more volume than I do, that's might the reason for several cases )

In my case Apple seemed to have removed without earlier notice the 20gb free space in the icloud. I noticed that the 2 uses who are power data usage users had lost of new data from my app (which was to deal with historical stock pricing ) and the others ones we just fine downloading the same data size.

I followed up with those 2, helping them to clean stuff up to let more space in icloud and voila, after downloading the data again it worked fine.