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