简体   繁体   English

核心数据数据未保存

[英]Core data data not saved

I am currently developing an application that uses Core Data to store data. 我目前正在开发一个使用Core Data来存储数据的应用程序。 The application synchronizes its content with a web server by downloading and parsing a huge XML file (about 40000 entries). 该应用程序通过下载和解析巨大的XML文件(约40000个条目)来将其内容与Web服务器同步。 The application allows the user to search data and modify it (CRUD). 该应用程序允许用户搜索数据并进行修改(CRUD)。 The fetch operations are too heavy, that is why i decided to use the following pattern : 提取操作太繁琐,这就是为什么我决定使用以下模式的原因:

"One managed object context for the main thread (NSMainQueueConcurrencyType) in order to refresh user interface. The heavy fetching and updates are done through multiple background managed object contexts (NSPrivateQueueConcurrencyType). No use of children contexts". “主线程的一个托管对象上下文(NSMainQueueConcurrencyType)以刷新用户界面。繁重的获取和更新是通过多个后台托管对象上下文(NSPrivateQueueConcurrencyType)完成的。不使用子上下文。”

I fetch some objects into an array (let us say array of "users"), then i try to update or delete one "user" (the object "user" is obtained from the populated array)in a background context and finally i save that context. 我将一些对象提取到一个数组中(让我们说“用户”数组),然后在后台上下文中尝试更新或删除一个“用户”(对象“用户”是从填充的数组中获取),最后我保存了那个背景。

I am listening to NSManagedObjectContextDidSaveNotification and merge any modifications with my main thread managed object context. 我正在听NSManagedObjectContextDidSaveNotification并将任何修改与我的主线程托管对象上下文合并。

Every thing works fine except when i relaunch my application i realize that none of the modifications has been saved. 一切正常,除非重新启动应用程序时,我意识到没有保存任何修改。

Here is some code to explain the used pattern 这是一些代码来解释使用的模式

  1. Main managed object context : 主管理对象上下文:

     -(NSManagedObjectContext *)mainManagedObjectContext { if (_mainManagedObjectContext != nil) { return _mainManagedObjectContext; } NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; _mainManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; [_mainManagedObjectContext setPersistentStoreCoordinator:coordinator]; return _mainManagedObjectContext; 

    } }

  2. Background managed object context : 后台管理对象上下文:

     -(NSManagedObjectContext *)newManagedObjectContext { NSManagedObjectContext *newContext; NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; newContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; [newContext performBlockAndWait:^{ [newContext setPersistentStoreCoordinator:coordinator]; }]; return newContext; 

    } }

  3. Update a record : 更新记录:

     -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ FootBallCoach *coach = [_coaches objectAtIndex:indexPath.row]; coach.firstName = [NSString stringWithFormat:@"Coach %i",indexPath.row]; NSManagedObjectContext *context = [[SDCoreDataController sharedInstance] newManagedObjectContext]; [context performBlock:^{ NSError *error; [context save:&error]; if (error) { NSLog(@"ERROR SAVING : %@",error.localizedDescription); } dispatch_async(dispatch_get_main_queue(), ^{ [self refreshCoaches:nil]; }); }]; 

    } }

Am i missing any thing ? 我有什么想念的吗? should i save my main managed object context after saving the background context ? 保存背景上下文后,是否应该保存主托管对象上下文?

If your context is configured with a persistent store coordinator, then save should write data to the store. 如果您的上下文配置有持久性存储协调器,则保存应将数据写入存储。 If your context is configured with another context as parent, then save will push the data to the parent. 如果您的上下文配置了另一个上下文作为父级,则保存会将数据推送到父级。 Only when the last parent, the one that is configured with persistent store coordinator is saved, is the data written to the store. 仅当最后一个父级(使用持久性存储协调器配置的父级)被保存时,数据才被写入到存储中。

  1. Check that your background context is really configured with persistent store coordinator. 检查您的背景上下文是否已使用持久性存储协调器进行了实际配置。
  2. Check the return value and possible error of the -save: . 检查-save:的返回值和可能的错误。
  3. Make sure you work with your background context via -performBlock...: methods. 确保通过-performBlock...:方法处理背景上下文。

UPDATE UPDATE

Each time you call your -newManagedObjectContext method, a new context is created. 每次调用-newManagedObjectContext方法时,都会创建一个新的上下文。 This context knows nothing about FootBallCoach object you're updating. 此上下文对您要更新的FootBallCoach对象一无所知。 You need to save the same context FootBallCoach object belongs to. 您需要保存与FootBallCoach对象所属的上下文相同的上下文。

Don't forget that each object belongs to one and only one context. 不要忘记,每个对象都属于一个且只有一个上下文。

Also make sure you hold a strong reference to a context whose objects you're using. 另外,请确保您对所使用对象的上下文拥有强大的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM