Reputation: 139
I'm relatively new to iOS development and it is my first time trying to use the CoreData.framework
.
I created a very simple DB with "sqlitebrowser" and copied it into my project in XCode. I can see the file is included in the app bundle. I tried to access the DB in the AppDelegate
class, following method:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
//For now using the `SQLite` store generate manually. Make sure to open the
//store in read only mode since we cannot modify files in the bundle.
NSURL *storeURL = [[NSBundle mainBundle] URLForResource:@"MatchWatcher" withExtension:@"sqlite"];
//Should be like below
NSDictionary *storeOptions = @{NSReadOnlyPersistentStoreOption:@YES, NSSQLitePragmasOption: @{@"journal_mode":@"DELETE"} };
// NSDictionary *storeOptions = @{NSSQLitePragmasOption: @{@"journal_mode" : @"DELETE"} };
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if(![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:storeOptions
error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
return _persistentStoreCoordinator;
}
As you can see, I try to use the storeOptions variable that includes the ReadOnly opening of the DB, but this gives me the following exception:
2015-07-06 14:15:18.372 MatchWatcher[27495:3849154] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/john/Library/Developer/CoreSimulator/Devices/F8C9B7DC-3C1D-495C-8FB6-80A9EA5976BB/data/Containers/Bundle/Application/CA64DE15-428B-4356-BB2B-066258E5FBAE/MatchWatcher.app/MatchWatcher.sqlite options:{ NSReadOnlyPersistentStoreOption = 1; NSSQLitePragmasOption = { "journal_mode" = DELETE; }; } ... returned error Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn’t be completed. (Cocoa error 257.)" UserInfo=0x7fa38a73cbb0 {NSUnderlyingException=Cannot create a new database file with the read only option at path: /Users/john/Library/Developer/CoreSimulator/Devices/F8C9B7DC-3C1D-495C-8FB6-80A9EA5976BB/data/Containers/Bundle/Application/CA64DE15-428B-4356-BB2B-066258E5FBAE/MatchWatcher.app/MatchWatcher.sqlite} with userInfo dictionary { NSUnderlyingException = "Cannot create a new database file with the read only option at path: /Users/john/Library/Developer/CoreSimulator/Devices/F8C9B7DC-3C1D-495C-8FB6-80A9EA5976BB/data/Containers/Bundle/Application/CA64DE15-428B-4356-BB2B-066258E5FBAE/MatchWatcher.app/MatchWatcher.sqlite"; } 2015-07-06 14:15:18.373 StockWatcher[27495:3849154] Unresolved error Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn’t be completed. (Cocoa error 257.)" UserInfo=0x7fa38a73cbb0 {NSUnderlyingException=Cannot create a new database file with the read only option at path: /Users/john/Library/Developer/CoreSimulator/Devices/F8C9B7DC-3C1D-495C-8FB6-80A9EA5976BB/data/Containers/Bundle/Application/CA64DE15-428B-4356-BB2B-066258E5FBAE/MatchWatcher.app/MatchWatcher.sqlite}, { NSUnderlyingException = "Cannot create a new database file with the read only option at path: /Users/john/Library/Developer/CoreSimulator/Devices/F8C9B7DC-3C1D-495C-8FB6-80A9EA5976BB/data/Containers/Bundle/Application/CA64DE15-428B-4356-BB2B-066258E5FBAE/MatchWatcher.app/MatchWatcher.sqlite"; }
Any ideas of what I'm doing wrong? Some help would be appreciated! Thanks.
Upvotes: 1
Views: 1195
Reputation: 46718
The SQLite file that Core Data uses is structured in a closed way. It is not meant to be used against a SQLite file that you have developed outside of Core Data.
Core Data is designed to be the model of your application. Treating it as a relational database tool will cause you issues. Use it as the model of your application and then let it persist to disk. Not the other way around.
Upvotes: 1