【问题标题】:Custom Migration corrupts db自定义迁移损坏数据库
【发布时间】:2018-03-24 23:19:48
【问题描述】:

当我尝试将旧数据库迁移到新数据库时,一切正常。但是当我尝试访问数据库时,它说它已损坏或格式错误。它只发生在 iOS11 中。

我收到此错误 -

error: exception handling request: <NSSQLRelationshipFaultRequestContext: 0x1c4cbc9e0> , Fatal error.  The database at /var/mobile/Containers/Data/Application/743F0953-2F57-4B9F-931A-0E0AEF9E8D0A/Documents/Main.db is corrupted.  SQLite error code:11, 'database disk image is malformed' with userInfo of {
    NSFilePath = "/var/mobile/Containers/Data/Application/743F0953-2F57-4B9F-931A-0E0AEF9E8D0A/Documents/Main.db";
    NSSQLiteErrorDomain = 11;
}

当我尝试访问数据库时,我得到 -

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Object's persistent store is not reachable from this NSManagedObjectContext's coordinator'

这是我的迁移代码 -

NSError     *error = nil;
    NSMappingModel                  *mapping = [NSMappingModel inferredMappingModelForSourceModel: oldModel destinationModel: self error: &error];
    if (error) {
        LOG(@"Error while inferring mapping model: %@", error);
        return NO;
    }
    NSString                        *newContextPath = [contextPath stringByAppendingPathExtension: @"tmp"];
    NSValue                         *classValue = [[NSPersistentStoreCoordinator registeredStoreTypes] objectForKey: NSSQLiteStoreType];
    Class                           sqliteStoreClass = (Class)[classValue pointerValue];
    Class                           sqliteStoreMigrationManagerClass = [sqliteStoreClass migrationManagerClass];
    NSURL                           *srcURL = [NSURL fileURLWithPath: contextPath], *dstURL = [NSURL fileURLWithPath: newContextPath];
    NSMigrationManager              *manager = [[sqliteStoreMigrationManagerClass alloc] initWithSourceModel: oldModel destinationModel: self];

    @try {
        if (![manager migrateStoreFromURL: srcURL type:NSSQLiteStoreType options:nil withMappingModel:mapping toDestinationURL: dstURL destinationType:NSSQLiteStoreType destinationOptions:nil error:&error]) {
            LOG(@"Migration failed %@", error);
            return NO;
        }
    } @catch (NSException *exception) {
        LOG(@"Exception: %@", exception);
        return NO;
    }
    if (![[NSFileManager defaultManager] removeItemAtPath: contextPath error: &error]) {
        LOG(@"Error removing old database: %@", error);
        return NO;
    }

    if (![[NSFileManager defaultManager] moveItemAtPath: newContextPath toPath: contextPath error: &error]) {
        LOG(@"Error renaming/moving new database: %@", error);
        return NO;
    }
        LOG(@"- Context Migration: Complete");

【问题讨论】:

    标签: ios objective-c iphone core-data core-data-migration


    【解决方案1】:

    我终于发现,我只删除了 .db 文件,而不是 .shm 和 .wal 文件。这对 iOS10 及更低版本没问题,但不知何故损坏了 iOS11 中的数据库。因此,删除了旧的 .shm 和 .wal 文件解决了这个问题。

    如果有人为出了什么问题而苦苦挣扎,这可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-04-15
      • 2013-08-14
      • 2019-07-12
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多