2

I get this error when running on the device, not the simulator (now).

I did get the same error on simulator as well and implemented the solution from this thread: enter link description here

with this code:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil) {
    return __persistentStoreCoordinator;
}

//    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"engliah_FamQuiz.sqlite"];

//=== USE DATABASE FROM MAIN BUNDLE ===//
NSURL *storeURL = [[NSBundle mainBundle] URLForResource:kNewDB withExtension:@"sqlite"];

// Use this for source store - ensures you don't accidentally write to the entities
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:1]
                                                     forKey:NSReadOnlyPersistentStoreOption];
// Make sure the WAL mode for Core Data is not enabled
   options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} }; <<< ADDED THIS LINE

//    NSDictionary *options = @{NSReadOnlyPersistentStoreOption : @YES, NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} };

NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

This solved the problem in the simulator but the app crash on the device with the following error:

2014-06-21 15:23:49.828 xxxx[30224:60b] Attempt to add read-only file at path file:///var/mobile/Applications/236047B5-0B0F-443D-ABCF-DCABA0166713/xxxx.app/yyyyy.sqlite read/write. Adding it read-only instead. This will be a hard error in the future; you must specify the NSReadOnlyPersistentStoreOption.
2014-06-21 15:23:49.838 xxxx[30224:60b] CoreData: error: (14) I/O error for database at /var/mobile/Applications/236047B5-0B0F-443D-ABCF-DCABA0166713/xxxx.app/yyyy.sqlite.  SQLite error code:14, 'unable to open database file'
2014-06-21 15:23:49.845 xxxx[30224:60b] Unresolved error Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x16e48d80 {NSUnderlyingException=I/O error for database at /var/mobile/Applications/236047B5-0B0F-443D-ABCF-DCABA0166713/xxxx.app/yyyyy.sqlite.  SQLite error code:14, 'unable to open database file', NSSQLiteErrorDomain=14}, {
NSSQLiteErrorDomain = 14;
NSUnderlyingException = "I/O error for database at /var/mobile/Applications/236047B5-0B0F-443D-ABCF-DCABA0166713/xxxx.app/yyyy.sqlite.  SQLite error code:14, 'unable to open database file'";

In simulator there is no error. Also, the first error is also new.

Deployment target: 6.1

Community
  • 1
  • 1
PeterK
  • 4,243
  • 4
  • 44
  • 74
  • After writing my answer I saw that you added the same code as an comment in your question: `// NSDictionary *options = @{NSReadOnlyPersistentStoreOption : @YES, NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} };`. - So which is your actual code now? – Martin R Jun 21 '14 at 13:55
  • I assume that the SQLite file is bundled with the app. Did you also disable the WAL-mode when *creating* the database? – Martin R Jun 21 '14 at 14:32
  • @MartinR no i did not disable the WAL when creating the DB, will test that now. – PeterK Jun 21 '14 at 16:33
  • @MartinR that fixed the problem, a BIG thank you for your help. Please add this as an answer so i can mark it solved. I still not understand why it worked in the simulator but not the device. – PeterK Jun 21 '14 at 16:53

1 Answers1

5

Your code

NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:1]
                                                forKey:NSReadOnlyPersistentStoreOption];
options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} };

sets options to a dictionary containing the NSSQLitePragmasOption key only, but not the NSReadOnlyPersistentStoreOption key, because the second line overwrites the dictionary assigned in the first line.

What you probably want is

NSDictionary *options = @{ 
    NSReadOnlyPersistentStoreOption : @YES,
    NSSQLitePragmasOption: @{@"journal_mode":@"DELETE"}
};

to create an options directory containing both keys.

In addition, you have also to disable the WAL-mode when creating the SQLite file that is bundled with the application.

The problem does not occur in the Simulator because the application directory is read-write in the Simulator, but read-only on the device.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • thanks, unfortunately i get the same error in the simulator this time :-( – PeterK Jun 21 '14 at 14:04
  • The comment from MartinR, which was the solution: I assume that the SQLite file is bundled with the app. Did you also disable the WAL-mode when creating the database? – Martin R 2 hours ago – PeterK Jun 21 '14 at 17:25