自动轻量级迁移适用于本地存储,但iCloud存储"丢失"所有遗留数据

时间:2014-07-04 10:07:46

标签: core-data ios7 icloud core-data-migration

我用这个撕掉我的头发。 我在iTunes上有一个应用程序,我在iOS7.0上添加了iCloud支持到去年年底(10月' 13) 本周我决定为App编写一个新功能,它需要xcdatamodel中的新实体。一个非常简单的改变/添加。应该对当前数据集没有影响。 我创建了一个新的v2 xcdatamodel并将其设置为Current Model版本,编译并运行,如果我在iPad上关闭了iCloud,它可以正常工作。我看到以前保存的数据。 在iCloud开启的情况下再次运行它,我得到一张没有数据的空白表。 没有错误消息,没有。 希望有人可以对我在这里做错的事情有所了解:

 - (NSManagedObjectModel *)managedObjectModel {
    if (__managedObjectModel != nil) {
        return __managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"UserData" withExtension:@"momd"];
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return __managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    NSError *error = nil;
    BOOL success = NO;

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

    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
    NSPersistentStoreCoordinator *psc = __persistentStoreCoordinator;

    NSString *iCloudEnabledAppID = @"C3FUPX46ZG~com~software~App";
    NSString *dataFileName = @"UserData.sqlite";
    NSString *iCloudDataDirectoryName = @"CoreData.nosync";
    NSString *iCloudLogsDirectoryName = @"CoreDataLogs";
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *localStore = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:dataFileName];
    NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier:nil];

    if (iCloud && ([UserDefaults getIsiCloudOn])) {
        // This iCloud storage fails to migrate.
        NSURL *iCloudLogsPath = [NSURL fileURLWithPath:[[iCloud path] stringByAppendingPathComponent:iCloudLogsDirectoryName]];

        if([fileManager fileExistsAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName]] == NO) {
            NSError *fileSystemError;
            [fileManager createDirectoryAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName]
                   withIntermediateDirectories:YES
                                    attributes:nil
                                         error:&fileSystemError];
            if(fileSystemError != nil) {
                NSLog(@"Error creating database directory %@", fileSystemError);
            }
        }

        NSString *iCloudData = [[[iCloud path]
                                 stringByAppendingPathComponent:iCloudDataDirectoryName]
                                stringByAppendingPathComponent:dataFileName];

        NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : @YES,
                                  NSInferMappingModelAutomaticallyOption : @YES,
                                  NSPersistentStoreUbiquitousContentNameKey : iCloudEnabledAppID,
                                  NSPersistentStoreUbiquitousContentURLKey : iCloudLogsPath
                                  };

        success = [psc addPersistentStoreWithType:NSSQLiteStoreType
                                    configuration:nil
                                              URL:[NSURL fileURLWithPath:iCloudData]
                                          options:options
                                            error:&error];

        if (!success) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        }
    } else {
        // This local storage migrates automatically just fine.
        NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : @YES,
                                  NSInferMappingModelAutomaticallyOption : @YES
                                  };

        success = [psc addPersistentStoreWithType:NSSQLiteStoreType
                                    configuration:nil
                                              URL:localStore
                                          options:options
                                            error:&error];

        if (!success) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        }
    }

    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:kCoreDataChangeNotification object:self userInfo:nil];
    });

    return __persistentStoreCoordinator;
}

更新:切换核心数据调试& iCloud调试/日志记录。 迁移适用于本地和iCloud中。日志是相同的,以:

结尾
  

CoreData:annotation :( migration)推断数据模型之间的映射模型...   CoreData:注释:(迁移)就地迁移在0.03秒内成功完成   -PFUbiquitySwitchboardEntryMetadata setUseLocalStorage :: CoreData:Ubiquity:mobile~F9AC6EB1   使用本地存储:1

使用iCloud存储&它上面的调试似乎会导致延迟,当它消失时,我会短暂地看到我保存的数据大约10秒。 就在它消失之前,调试就吐了出来:

  

CoreData:annotation :( migration)推断数据模型之间的映射模型...   使用本地存储:0

iCloud日志非常庞大,这就是我没有在这里发布的原因。从我可以看到我有超过400个日志文件和iCloud似乎正在进行某种同步。如果我打开App并打开iPad几个小时,我仍然会看到一个空的数据集。所以它不是等待同步赶上的情况。即使调试了......我仍然感到茫然。

0 个答案:

没有答案