我更改了现有的核心堆栈,我的sqlite文件在创建后不再更新。我能够使用核心数据保存和获取数据,但sqlite文件始终为空(sqlite-shm文件会更新)。我的实施错了吗?我需要做些什么来更新我的sqlite文件?
//头文件
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
@synthesize masterManagedObjectContext = __masterManagedObjectContext;
@synthesize mainManagedObjectContext = __mainManagedObjectContext;
//Managed object context
-(NSManagedObjectContext *)masterManagedObjectContext{
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
__masterManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[__masterManagedObjectContext setPersistentStoreCoordinator:coordinator];
}
return __masterManagedObjectContext;
}
- (NSManagedObjectContext *)mainManagedObjectContext {
if (__mainManagedObjectContext != nil) {
return __mainManagedObjectContext;
}
__mainManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[__mainManagedObjectContext setParentContext:self.masterManagedObjectContext];
return __mainManagedObjectContext;
}
- (NSManagedObjectContext *)workerManagedObjectContext {
NSManagedObjectContext *tempMOContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
tempMOContext.parentContext = [self mainManagedObjectContext];
return tempMOContext;
}
//Save methods
- (void)saveWorkerContext:(NSManagedObjectContext *)context {
NSError *error;
[context save:&error];
if (!error) {
[self saveMainContext];
}
}
- (void)saveMainContext {
[self.mainManagedObjectContext performBlock:^{
NSError *error = nil;
[self.mainManagedObjectContext save:&error];
if(!error){
//Write to disk after saving on the main UI context
[self saveMasterContext];
}
}];
}
- (void)saveMasterContext {
[self.masterManagedObjectContext performBlock:^{
NSError *error = nil;
[self.masterManagedObjectContext save:&error];
if(error){
NSLog(@"CORE DATA MASTER CONTEXT ERROR : %@", error);
}
}];
}
-(NSManagedObjectModel *)managedObjectModel {
if (__managedObjectModel != nil) {
return __managedObjectModel;
}
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"MyResource"
ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
NSString *modelPath = [bundle pathForResource:@"MyData"
ofType:@"momd"];
//NSLog(@"Bundle modelURL:%@",modelPath);
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return __managedObjectModel;
}
-(NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (__persistentStoreCoordinator != nil) {
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyData.sqlite"];
//NSLog(@"storeURL:%@",storeURL);
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
//实施文件
-(void)saveDictionary:(NSMutableDictionary *)myDictionary{
NSManagedObjectContext *context = [self workerManagedObjectContext];
[context performBlockAndWait:^{
NSMutableDictionary *tmpDic = myDictionary;
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
Entity *myEntity;
if ([fetchedObjects count]>0) {
myEntity = [fetchedObjects lastObject];
}
else{
myEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:context];
}
myEntity.value = tmpDic[@"value1"]!=nil?tmpDic[@"value1"]:@"nothing";
[self saveWorkerContext:context];
}];
}
//尝试保存数据的示例
{{1}}
答案 0 :(得分:0)
我需要做些什么才能让它发挥作用?
但你也说过
我能够使用核心数据保存和获取数据......
" work"的定义核心数据。如果可以的话,你就完成了。
SQLite shm文件是SQLite日志。它是SQLite如何工作的实现细节。如果您有兴趣,可以阅读how SQLite manages its files,但这与您正在做的事情无关。
答案 1 :(得分:0)
在添加持久存储时,只需要生成sqlite文件的时间使用以下选项:
@{NSSQLitePragmasOption: @{@"journal_mode": @"DELETE"}}
[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil
URL:self.localStoreURL
options:@{NSSQLitePragmasOption: @{@"journal_mode": @"DELETE"}}
error:nil];
然后运行应用程序,退出,转到应用程序文件夹,在Documents
中,您的文件将被更新。我希望这会对你有所帮助。