Jas*_*ers 6 sqlite core-data core-data-migration ios
我目前正在编写应用程序的下一个版本.
在旧版本中,没有CoreData sqlite
在新版本中,我们有一个本地CoreData sqlite数据库.
当我从头开始安装新版本的应用程序时,没有问题,商店就在那里,我可以查询.
但是,当我在具有上一版本的手机上安装应用程序时,我的查询会返回,但没有结果.
当我查看日志时,控制台中没有任何内容,并且不会返回任何错误.
#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 {
//
}
}
Run Code Online (Sandbox Code Playgroud)
苏。不确定您是否这样做,但请尝试以下操作:
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
NSError* error;
[managedObjectContext.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:self.storeUrl
options:options // this is required to migrate some core data model attributes
error:&error];
Run Code Online (Sandbox Code Playgroud)
基本上必须在设置时告诉它在选项中使用此迁移内容。