I have a list app that I'm working on that I want the core data to be pre-populated, so when a user launches the app for the first time, tableview is populated with some default lists, rather than empty tableview. I searched through a lot of questions and followed their steps, but the app always launches with empty tableview.
Here are the insufficient steps I'm taking. I'll also leave some links to where I got these steps from, if that helps.
- Disable .sqlite-wal, and .sqlite-shm, to save data directly into .sqlite.
NSDictionary *options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} };
Link - Run the app on a simulator and populate .sqlite file the way I want it to be populated. I used the Firefox SQLite Manager to make sure that .sqlite file is populated.
- Find the .sqlite file location with
NSLog(@"StoreURL Path %@", storeURL.path);
, and copy the file from Finder into app directory, with Destination box unchecked. Link - at "Doctor, you’re needed in Xcode" In
persistentStoreCoordinator
in AppDelegate Link- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory]
URLByAppendingPathComponent:@"App_Name.sqlite"];
NSLog(@"StoreURL Path %@", storeURL.path);
// If the expected store doesn't exist, copy the default store.
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:[storeURL path]]) {
NSURL *defaultStoreURL = [NSURL fileURLWithPath:[[NSBundle
mainBundle] pathForResource:@"App_Name" ofType:@"sqlite"]];
if (defaultStoreURL) {
[fileManager copyItemAtURL:defaultStoreURL toURL:storeURL
error:NULL];
}
NSError *error = nil;
// Options for migration
NSDictionary *options = @{NSMigratePersistentStores
AutomaticallyOption: @(YES),
NSInferMappingModelAutomaticallyOption : @(YES) };
//NSDictionary *options = @{ NSSQLitePragmasOption :
//@{@"journal_mode": @"DELETE"} };
if (![_persistentStoreCoordinator addPersistentStoreWithType:
NSSQLiteStoreType configuration:nil URL:storeURL options:
options error:&error]) {
// Handle error
}
return _persistentStoreCoordinator;
}
I've also tried the below method, from the CoreDataBooks sample code, with no luck.
// If the expected store doesn't exist, copy the default store.
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:[storeURL path]]) {
NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:
@"CoreDataBooks" withExtension:@"CDBStore"];
if (defaultStoreURL) {
[fileManager copyItemAtURL:defaultStoreURL toURL:storeURL
error:NULL];
}
}
Also, I only want to populate the tableview with few lists. Is it better to create a flag for first launch, and populate with code?
I really don't know what I missed. Please help me with this. Your help will be appreciated. Thanks
Aucun commentaire:
Enregistrer un commentaire