0

sqlite データベースをコア データ sqlite にプリロードしようとしています。最初にシミュレーターから sqlite を取得し、次にいくつかのデータを入力しました。

問題は、ルート ディレクトリにある塗りつぶされた .sqlite から coreData .sqlite にデータを取得できないことです (このファイルはシミュレータに格納されています)。

Apple のサンプルCoreDataBooksを試してみましたが、うまくいきません。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{

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

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyData.sqlite"];


    NSFileManager *fileManager = [NSFileManager defaultManager];
    // If the expected store doesn't exist, copy the default store.
    if (![fileManager fileExistsAtPath:[storeURL path]]) {
        NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"MyData" withExtension:@"sqlite"];
        if (defaultStoreURL) {
            [fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
        }
    }

    NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption: @YES, NSInferMappingModelAutomaticallyOption: @YES};
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

    NSError *error;
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {

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

    return __persistentStoreCoordinator;
}


- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
4

1 に答える 1

1

MyData.sqlite ファイルの [ターゲット メンバーシップ] チェックボックスを選択する必要があります。選択しないと、アプリケーション バンドルにコピーされません。

于 2013-10-18T18:26:24.410 に答える