boochan224
boochan224

Reputation: 381

Core Data sqlite file embedded in my app?

I'm trying to make a little dictionary on my app by using Core Data.When you use a Master-Detail application, xxx.sqlite file gets created in the user's Documents folder. Now before I start the application for the first time, I changed the code as below because I wanted the xxx.sqlite file in the app:

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

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

    NSString* path= [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CoreDataTest.sqlite"];

NSURL* storeURL = [[NSURL alloc] initFileURLWithPath:path];

I thought the last two line would make a xxx.sqlite file in my app project folder. Actually, it didn't. But the app works fine. This means that the xxx.sqlite file is embedded in the app itself? Thank you for your time.

Upvotes: 0

Views: 747

Answers (2)

βhargavḯ
βhargavḯ

Reputation: 9836

Try with this code. Write this inside AppDelegate.m

- (NSManagedObjectContext *) managedObjectContext {
    if (managedObjectContext != nil) {
        return managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }

    return managedObjectContext;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }
    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
                                               stringByAppendingPathComponent: @"ProjectManagement.sqlite"]];
    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
                                   initWithManagedObjectModel:[self managedObjectModel]];
    if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                 configuration:nil
                                                           URL:storeUrl
                                                       options:nil error:&error]){
        /*Error for store creation should be handled in here*/
    }

    return persistentStoreCoordinator;
}

- (NSManagedObjectModel *)managedObjectModel {
    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];

    return managedObjectModel;
}

- (NSString*)applicationDocumentsDirectory{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
}

Upvotes: 0

Nick Banks
Nick Banks

Reputation: 111

If, as I understand your question, you would like to include a default database with your app (i.e. the .sqlite file will be included as an asset of your project, then you need to copy it from the app bundle into your documents directory at startup).

Then call this function from your didFinishLaunchingWithOptions method in your app delegate.

- (void) copyDefaultDB
{
    // If we are running the app for the first time, then copy our default database across from our app directory
    // into our user directory 
    NSString *filePath;

    // Get the path to the documents directory and append the databaseName
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex: 0];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    filePath = [documentsDir stringByAppendingPathComponent: @"CoreDataFileTest.sqlite"];

    // If the database already exists then return without doing anything
    if (![fileManager fileExistsAtPath: filePath])
    {
        // Now copy the new shiny one in

        NSString *filePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"CoreDataFileTest.sqlite"];

        // Copy the database from the package to the users filesystem
        [fileManager copyItemAtPath: filePathFromApp 
                             toPath: filePath 
                              error: nil];
    }
}

Upvotes: 1

Related Questions