2017-10-12 3 views
0

古いデータベースを新しいデータベースに移行しようとすると、すべて正常に実行されます。しかし、私がデータベースにアクセスしようとすると、そのデータベースは壊れているか、または不正な形式であると言います。 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; 
} 

私はDBにアクセスしようとすると、私が取得 -

*** 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"); 

答えて

0

やっと見つけました私は.dbファイルだけを削除していましたが、.shmファイルと.walファイルは削除していませんでした。これはiOS10以降では問題ありませんでしたが、iOS11ではdbが何らかの形で破損しています。したがって、古い.shmファイルと.walファイルの両方が削除されて問題が解決されました。

何がうまくいかないのであれば、これは助けになるかもしれません。

関連する問題