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

 loassde_392 发布于 2023-01-06 13:03

我用这个撕掉了我的头发.我在iTunes上有一个应用程序,我在去年年底(2013年10月)在iOS7.0上添加了iCloud支持本周我决定为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:注释:(迁移)推断数据模型之间的映射模型... CoreData:annotation :(迁移)就地迁移在0.03秒内成功完成-PFUbiquitySwitchboardEntryMetadata setUseLocalStorage :: CoreData:Ubiquity:mobile~F9AC6EB1使用本地存储: 1

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

CoreData:注释:(迁移)推断数据模型之间的映射模型...使用本地存储:0

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

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有