Reputation: 11749
Can any one tell me what is wrong with the code below? It always crashes at the line containing “addPersistentStoreWithType”.
Any suggestion will be welcome.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
// Create the coordinator and store
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Core_Data.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:[storeURL path]]) {
NSURL *defaultStoreURL=[[NSBundle mainBundle] URLForResource:@"Core_Data" withExtension:@"sqlite"];
if (defaultStoreURL) {
// By default we use what is in the bundle read-only.
storeURL=defaultStoreURL;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![fileManager fileExistsAtPath:[storeURL path]])
NSLog(@"RO-DB NOT FOUND");
else NSLog(@"RO-DB FOUND");
NSError *error = nil;
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:@{NSSQLitePragmasOption:@{ @"journal_mode":@"DELETE" },
NSReadOnlyPersistentStoreOption:[NSNumber numberWithBool:YES]}
error:&error]) {
NSLog(@"RO-DB NOT OPENED");
}
return _persistentStoreCoordinator;
}
}
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
// Report any error we got.
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
dict[NSLocalizedFailureReasonErrorKey] = failureReason;
dict[NSUnderlyingErrorKey] = error;
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
The rest of the code is nothing more than what is generated by Xcode when building a new project using a Single View template with Core Data.
Upvotes: 0
Views: 709
Reputation: 11749
Here is the thing I was doing wrong: I did not copy all the necessary files to the bundle. I had tried with one or two, but there are three: File.sqlite, File.sqlite-shm, File.sqlite-wal.
Hoping this may help someone else in the future.
Somebody with a deeper understanding of Core Data may want to comment further.
Upvotes: 0
Reputation: 80265
You cannot add a read-write store that is in the bundle. The bundle is read only.
Instead, copy the sqlite file to the documents directory first and use that URL to add the store to the store coordinator.
I do not think that journal_mode "delete" and NSReadOnlyPersistentStoreOption
are compatible. (Does not seem to make any sense, anyway.) Maybe a journal_mode "off" or no journal_mode option would be more promising.
Upvotes: 1