Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

iphone - More iCloud Core Data synching woes

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...