0

I am developing an iOS app that I want to ship with a pre-filled Core Data database. I filled the database in the simulator once, disabled this code, and everytime I run the app in the simulator, I perform a check in my first view controller to see how many entities there are in my DB:

- (void) printNumberOfWords{

FWHAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context =[appDelegate managedObjectContext];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSManagedObjectContext *managedObjectContext = context;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Word" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSError *error = nil;
NSUInteger count = [managedObjectContext countForFetchRequest:request error:&error];

 if (!error){
    NSLog(@"in total %d words",count);
 }
 else
    NSLog(@"error with counting words");
 }
}

this always prints out "In total 1083 words", what is correct - I expect there to be 1083 words.

So my next step was to add this file to my Xcode project so that I can bundle it with my app. I located the file in my /Users/myusername/Library/Application Support/iPhone Simulator/7.1/Applications/appname/Documents folder, copied it to my Xcode project ( checked the "copy resources if needed" and "add to target" fields), and then I modified my AppDelegate.m like this (copied form the CoreDataBooks example provided by Apple) :

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{

if (_persistentStoreCoordinator != nil) {
    return _persistentStoreCoordinator;
}

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Fit_Wit_Hit.CDBStore"];

NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:[storeURL path]]) {
    NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"Fit_Wit_Hit" withExtension:@"CDBStore"];
    if (defaultStoreURL) {
        [fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
    }
}

NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption: @YES, NSInferMappingModelAutomaticallyOption: @YES};
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

NSError *error;
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {

    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

return _persistentStoreCoordinator;
}

I deleted the app from my simulator, ran it again (and the importing data code was still disabled, so I know that the database wouldn't be filled by my own code) , and when I reach the point where I check how many entities there are, it prints out "in total 978 words" - so, 105 entities less than I would expect!

Does anyone have any idea why this is happening? I miss some of my data, and I really don't know what I am doing wrong here.

UPDATE Had to disable the WAL-mode, the answer was here: How to disable WAL journal mode

Community
  • 1
  • 1
Danny Hobo
  • 574
  • 1
  • 5
  • 24
  • 3
    SQLLite stores have special files containing changes, if you only copy the single database file and not the other two. expected behavior. See for example: http://stackoverflow.com/questions/20062197/how-to-disable-wal-journal-mode on how to disable that mode – Volker Apr 17 '14 at 09:08
  • 3
    You copied only the .sqlite file? Did you see a .shm / .wal file? – Wain Apr 17 '14 at 09:09
  • wow, thanks a lot! yes, I needed to disable the WAL mode, and then it works. – Danny Hobo Apr 17 '14 at 09:18
  • I'll add my comment as answer so other users can see there is an answer more easily. – Volker Apr 17 '14 at 09:23
  • if you fixed it, answer the question yourself and accept your own answer – Max MacLeod Apr 17 '14 at 09:28
  • ok, will do that next time - wasn't familiar with these guidelines. for now I'll accept Volker's answer :) – Danny Hobo Apr 17 '14 at 10:02

1 Answers1

1

SQLite stores have special files containing changes, due to a change in iOS 7 and Mac OS X 10.9. The feature WAL journal_mode results in a wal file, in addition a shm file exists.

If you only copy the single database file and not the other two, you will see missing entries in some situations. This is expected behavior. See for example: stackoverflow.com/questions/20062197/… on how to disable WAL mode.

Volker
  • 4,640
  • 1
  • 23
  • 31