0

ここスタックで多くの説明とヘルプを見つけましたが、これまでのところ運がありません。

ほとんどの場合、myapp.sqlite (事前に入力したもの) はシミュレーターでは正常に動作しますが、iPad で実行すると空になります。

したがって、さまざまなことを試した後、これが私が得た最も近いものです:

  • sqlite db を Bundle にコピーしますが、Library フォルダーに移動します。

私の AppDelegate では、次のようにします。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    NSLog(@"Starting to save the DB to another location");

    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    NSString *targetPath = [libraryPath stringByAppendingPathComponent:@"myDB.sqlite"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:targetPath]) {
        // database doesn't exist in your library path... copy it from the bundle
        NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"myDB" ofType:@"sqlite"];
        NSError *error = nil;

        if (![[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:targetPath error:&error]) {
            NSLog(@"Error: %@", error);
        }
    }


    return YES;
}

次に、PersistentStoreCoordinator でこれを行います

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

    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    NSString *targetPath = [libraryPath stringByAppendingPathComponent:@"myDB.sqlite"];

//    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"naccApp.sqlite"];
//    NSURL *storeURLLocal = [[NSBundle mainBundle] URLForResource:@"myDB" withExtension:@"sqlite"];

    NSURL *storeURL = [NSURL URLWithString:targetPath];

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

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

    return _persistentStoreCoordinator;
}

DB を使用しようとすると (fetchRequest)、エラーが発生します。

CoreData SQL ストアはファイル URL のみをサポートします (get /var/mobile/Applications/2EB2AADD-DF9D-475F-A05E-BB138502471F/Library/myDB.sqlite)。

メッセージは明確ですが、ここでほとんどすべてのヘルプを試しましたが、まだ何もありません。

言うまでもなく、Core Data は初めてなので、無知をお許しください。

ああ、私はxCode 5を使用しています。

どうも

4

1 に答える 1