2
votes

Here's what I need: To have a sqlite file populated with example entities that I made on the iPhone simulator, and then copy that file when the app initially runs for all my users.

What I've done:

  1. I created a bunch of entries within the simulator.

  2. I found the sqlite file attached to my app within the iPhone Simulator iOS folder on the MAC.

  3. From the three files, .sqlite, .sqlite-shm, .sqlite-wal I simply copied the .sqlite file to my xCode project.

  4. When I ran the app, the .sqlite file showed up empty!

How do I fix this?

Thank you!

EDIT:

What significance does the .sqlite-wal and .sqlite-shm have?

Why do they exist and why did not exist prior to iOS7?

2
Did you google what they are?Wain

2 Answers

1
votes
  • first steps R OK but then U have to load the database

U need smth like this:

- (void)copyPreparedDatabase{
    __persistentStoreCoordinator = nil;

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"DATABASE.sqlite"];
    NSString *storePath = [storeURL path];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"DATABASE" ofType:@"sqlite"];
    if (defaultStorePath) {
        NSError *error = nil;

        if ([fileManager fileExistsAtPath:storePath]) {
            [fileManager removeItemAtPath:storePath error:&error];
        }
        [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:&error];

        NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
        if (![[NSFileManager defaultManager] setAttributes:fileAttributes ofItemAtPath:storePath error:&error]) {

        }
    }
}

And then U call it from - (NSPersistentStoreCoordinator *)persistentStoreCoordinator from AppDelelate.m

Advice: Do some custom switch like #define IMPORT_PREPARED_DATABASE

do it like this: if (![fileManager fileExistsAtPath:storePath] && !IMPORT_PREPARED_DATABASE) { //&& 1==2 [self copyPreparedDatabase];
}

so U can control when to build new prepared database or when to use existing one....

Note: When U build new prepared database sto simulator, copy database and paste it over the old one...

0
votes

This tutorial can be a big help.

You made almost everything, but you are missing an important step. You need to copy the sqlite to your xcode project then you need to check in your persistentStoreCoordinator if the sqlite file exists in your Documents area. If not, you need to copy it. Jump to "Doctor, you’re needed in Xcode" section in that tutorial :).