2

データがプリロードされた Sqlite ファイルがあります。Core Data を使用して、Sqlite ファイルからデータを読み取って表示しています。

これが私がデータを読んでいる方法です:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSURL *storeURL;


        NSString *path = [[NSBundle mainBundle] pathForResource:@"myAppname" ofType:@"sqlite"];
        storeURL = [NSURL fileURLWithPath:path];



    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    // This dictionary automatically updates and migrates the store if it changes
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                             [NSNumber numberWithBool: NO], NSReadOnlyPersistentStoreOption,
                             nil];

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

         Typical reasons for an error here include:
         * The persistent store is not accessible;
         * The schema for the persistent store is incompatible with current managed object model.
         Check the error message to determine what the actual problem was.


         If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

         If you encounter schema incompatibility errors during development, you can reduce their frequency by:
         * Simply deleting the existing store:
         [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

         * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
         @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}

         Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

         */
        if (error) {
            [myErrorAlert showError:error];
        }

        CLS_LOG(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _persistentStoreCoordinator;
}

ただし、sqliteデータエディターを使用してアプリの外部で行った新しいデータでSqliteファイルを更新する必要があります。

「myAppname.sqlite」ファイルをコピーして貼り付けてオーバーライドすると、次のエラーが発生します。

CoreData: error: (14) I/O error for database at /Users/Username/Library/Application Support/iPhone Simulator/7.1/Applications/45C373D7-8A54-48EF-BA3C-2E2C6AB7467F/myAppname.app/myAppname.sqlite.  SQLite error code:14, 'unable to open database file'

追加しようとしましNSDictionary *options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"}たが、それでも同じエラーが存在します。

また、ファイルへの参照を削除して新しいファイルを作成することを提案する回答もありましたが、その方法がわかりません。

何か案が?

編集

私はそれを機能させることができました。

iOS 6 シミュレーターで元のアプリを開きます。

Sqlite ファイルを新しいファイルに置き換え、iOS7 シミュレーターで実行するよりもアプリのみの iOS 6 シミュレーターを実行します。問題ない。

しかし、それでも他のコメントを知りたいです。

4

0 に答える 0