Search code examples
sqlitecore-dataios7

Crash when using NSReadOnlyPersistentStoreOption


I am using a read only sqlite database located in MainBundle for my app. In the PersistentStoreCoordinator I am loading the database with the following code:

    NSDictionary *storeOptions = @{NSReadOnlyPersistentStoreOption : [NSNumber numberWithBool:YES]};

NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self applicationManagedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[[NSBundle mainBundle] URLForResource:@"applications" withExtension:@"sqlite"] options:nil error:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error description]);
    abort();
}

In iOS7 this code crashing both in the simulator, and the device with the following error:

 CoreData: error: (14) I/O error for database at /var/mobile/Applications/4B81AEFE-03E6-4156-B52D-3452515FACAF/myapp.app/applications.sqlite.  SQLite error code:14, 'unable to open database file'
2014-03-22 20:45:34.346 GfxHotkeys3[1369:60b] CoreData: error: Encountered exception I/O error for database at /var/mobile/Applications/4B81AEFE-03E6-4156-B52D-3452515FACAF/myapp.app/applications.sqlite.  SQLite error code:14, 'unable to open database file' with userInfo {
    NSFilePath = "/var/mobile/Applications/4B81AEFE-03E6-4156-B52D-3452515FACAF/myapp.app/applications.sqlite";
    NSSQLiteErrorDomain = 14;
} while checking table name from store: <NSSQLiteConnection: 0x155b0fd0>
2014-03-22 20:45:34.372 GfxHotkeys3[1369:60b] Unresolved error Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x155b01b0 {NSUnderlyingException=I/O error for database at /var/mobile/Applications/4B81AEFE-03E6-4156-B52D-3452515FACAF/myapp.app/applications.sqlite.  SQLite error code:14, 'unable to open database file', NSSQLiteErrorDomain=14}, Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x155b01b0 {NSUnderlyingException=I/O error for database at /var/mobile/Applications/4B81AEFE-03E6-4156-B52D-3452515FACAF/GfxHotkeys3.app/applications.sqlite.  SQLite error code:14, 'unable to open database file', NSSQLiteErrorDomain=14}

passing nil in store options, will run the app in the simulator but not on the device (since it can't write on MainBundle).

I can solve this by copying the database into documents and loading from there, but I am wondering why its happening. I am using this option for a number of years now for loading read only sqlite from MainBundle, but in iOS 7 is crashing...

Any clues?

Many Thanks


Solution

  • To open a read-only store, you should use

    NSDictionary *storeOptions = @{NSReadOnlyPersistentStoreOption : @YES};
    

    instead of @NO as you did.