I am currently writing the next version of an app.
In the Old version, there was no CoreData sqlite
In the New version, we have a local CoreData sqlite database.
When I install the new version of the application from scratch, there is no problems, the store is there and I can query.
However, when I install the app on phone that has the previous version on it, my queries come back with no results.
When I look at the logs, there is nothing in the console, and no errors are returned.
#import "CoreDataHelper.h"
@implementation CoreDataHelper
@synthesize store = _store;
@synthesize coordinator = _coordinator;
#pragma mark -
#pragma mark - FILES
NSString *storeFileName = @"Reporting.sqlite";
#pragma mark -
#pragma mark - PATHS
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
- (NSURL *)applicationStoresDirectory {
NSURL *storesDirectory = [[NSURL fileURLWithPath:[self applicationDocumentsDirectory]]URLByAppendingPathComponent:@"Stores"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:[storesDirectory path]]) {
NSError *error = nil;
if ([fileManager createDirectoryAtURL:storesDirectory
withIntermediateDirectories:YES
attributes:nil
error:&error]) {
//File created
} else {
//Error
}
}
return storesDirectory;
}
- (NSURL *)storeURL {
return [[self applicationStoresDirectory] URLByAppendingPathComponent:storeFileName];
}
#pragma mark -
#pragma mark - SETUP
- (id)init {
if (self = [super init]) {
_model = [NSManagedObjectModel mergedModelFromBundles:nil];
_coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:_model];
_context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_context setPersistentStoreCoordinator:_coordinator];
}
return self;
}
- (void)loadStore {
if (_store) return; // Don't load store if it is already loaded
// to generate the database in the app launching comment next lines...
if(![self getFileExistence:storeFileName]){
// file URL in our bundle
NSURL *fileFromBundle = [[NSBundle mainBundle]URLForResource:@"FaultReporting" withExtension:@"sqlite"];
// Destination URL
NSURL *destinationURL = [[self applicationStoresDirectory] URLByAppendingPathComponent:@"FaultReporting.sqlite"];
// copy it over
[[NSFileManager defaultManager]copyItemAtURL:fileFromBundle toURL:destinationURL error:nil];
}
// end of comments
NSError *error = nil;
@try {
_store = [_coordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:[self storeURL]
options:@{ NSMigratePersistentStoresAutomaticallyOption: @(YES),
NSInferMappingModelAutomaticallyOption: @(YES)}
error:&error];
}
@catch (NSException *exception) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Error: %@, %@",error, [error userInfo]] delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
}
@finally {
//
}
}
loadStore
? What else can modify the instance variable_store
? IsloadStore
getting past the first line,if (_store)
? – Beatabeaten&error
parameter to a method and not checking to see if an error occurred, and there's another witherror:nil
. You should start by at least verifying that the code above is doing what you expect, and by looking to see what error messages, if any, the APIs are trying to send you. – Accomplished