Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will Core Data create the persistent store file for me?

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.

like image 743
dontWatchMyProfile Avatar asked Dec 30 '22 02:12

dontWatchMyProfile


1 Answers

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

like image 118
Massimo Cafaro Avatar answered Jan 05 '23 00:01

Massimo Cafaro