4

Please tell me: If I use Core Data in my iPhone app, I have basically two files. The mydatamodel.xcdatamodel file, and then I need an .sqlite file. Apple provides this code snippet:

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

    NSString *appDirPath = [self applicationDocumentsDirectory];
    NSString *storeFileName = @"mystore.sqlite";
    NSURL *storeUrl = [NSURL fileURLWithPath:[appDirPath stringByAppendingPathComponent:storeFileName]];

    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
        NSLog(@"Error: %@, %@", error, [error userInfo]);
        abort();
    }

    return persistentStoreCoordinator;
}

Will this create the file if it's not available already?

[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]

My app doesn't need initial data, because it will download it when the user launches the app.

3 Answers 3

7

Yes, Core Data will create the SQLite db just after the first launch of your application in your app delegate.

3
  • Perfect. Thank you. What if I provided that file already? Afaik I can't ship it directly into the documents dir. So I would first copy it there before attempting to create it, right? Feb 9, 2010 at 22:21
  • 1
    Correct. You would have a release version in your app bundle and copy it over.
    – gerry3
    Feb 9, 2010 at 22:22
  • If i did the tutorial for iOS on the developer website, and my app crashes saying there is no persistent store available. How could i fix this?
    – iProRage
    Feb 7, 2012 at 2:31
2

Yes. The boiler-plate Core Data stack code provided by Apple's templates will create the database file if it doesn't already exist.

1
  • If i did the tutorial for iOS on the developer website, and my app crashes saying there is no persistent store available. How could i fix this?
    – iProRage
    Feb 7, 2012 at 2:32
1

Yes, that method adds a new persistent store of a specified type at a given location, and returns the new store. Here is the documentation.

1
  • If i did the tutorial for iOS on the developer website, and my app crashes saying there is no persistent store available. How could i fix this?
    – iProRage
    Feb 7, 2012 at 2:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.