mercredi 26 août 2015

Core Data empty on preloaded sqlite import from main bundle

I am copying my preloaded sqlite file, sqlite-wal file, and sqlite-shm file from my main bundle, yet my app shows no data. I don't get any errors, and when I go look at the files in the Documents directory, their size is not 0. How can I get these files moved over?

Here is my persistentStore method:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *storePath = [documentsDirectory stringByAppendingPathComponent: @"MyApp.sqlite"];

    // Check if the sqlite store exists
    if (![[NSFileManager defaultManager] fileExistsAtPath:storePath]) {
        NSLog(@"File not found... copy from bundle");

        // copy the sqlite files to the store location.
        NSString *bundleStore = [[NSBundle mainBundle] pathForResource:@"MyApp" ofType:@"sqlite"];
        [[NSFileManager defaultManager] copyItemAtPath:bundleStore toPath:storePath error:nil];

        bundleStore = [[NSBundle mainBundle] pathForResource:@"MyApp" ofType:@"sqlite-wal"];
        storePath = [documentsDirectory stringByAppendingPathComponent: @"MyApp.sqlite-wal"];
        [[NSFileManager defaultManager] copyItemAtPath:bundleStore toPath:storePath error:nil];
        DLog(@"STOREPATHWAL:%@",storePath);

        bundleStore = [[NSBundle mainBundle] pathForResource:@"MyApp" ofType:@"sqlite-shm"];
        storePath = [documentsDirectory stringByAppendingPathComponent: @"MyApp.sqlite-shm"];
        [[NSFileManager defaultManager] copyItemAtPath:bundleStore toPath:storePath error:nil];
    }
    else {
        NSLog(@"File exists");
    }

    NSError *error = nil;
    NSString *failureReason = @"There was an error creating or loading the applications saved data.";
    //storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyApp.sqlite"];
   NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyApp.sqlite"];
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{ NSMigratePersistentStoresAutomaticallyOption : @YES, NSInferMappingModelAutomaticallyOption : @YES } error:&error]) {
        // Report any error we got.

        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
        dict[NSLocalizedFailureReasonErrorKey] = failureReason;
        dict[NSUnderlyingErrorKey] = error;
        error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
        // Replace this 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.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    return _persistentStoreCoordinator;
}

Aucun commentaire:

Enregistrer un commentaire