什么是线程安全的方式来保存上下文形式核心数据? [英] What would be a thread-safe way to save a context form Core Data?

查看:165
本文介绍了什么是线程安全的方式来保存上下文形式核心数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSOperationQueue 设置为 NSOperationQueueDefaultMaxConcurrentOperationCount 。它充满了 NSOperation 对象(到目前为止没有什么奇怪)。我继承了 NSOperation 以执行一些后台任务。

I have a NSOperationQueue set to NSOperationQueueDefaultMaxConcurrentOperationCount. It is filled with NSOperation objects (nothing weird so far). I subclassed the NSOperation to do some background tasks.


  1. 从互联网下载数据


  2. 解析数据以便我可以读取。

  3. 创建NSManagedObject:

  1. Download data from the internet.
  2. Parse the data so I can read it.
  3. Create a NSManagedObject:

[NSEntityDescription insertNewObjectForEntityForName:@ChannelinManagedObjectContext:context];

[NSEntityDescription insertNewObjectForEntityForName:@"Channel" inManagedObjectContext:context];

使用上下文保存。

[managedObjectContext save:& error]

[managedObjectContext save:&error]



我喜欢这一切都发生在背景所以UI不会被阻塞。我阅读了本文关于核心数据的并发性,以及据我所了解。最好的方法是在每个 NSOperation 中创建一个新的 NSManagedObjectContext ,但是共享同一个持久存储协调器。

I like this all to happen in the background so the UI won't get blocked. I read this article about concurrency with core data, and as far as I understood it. The best way would be to create a new NSManagedObjectContext in every NSOperation, but share the same persistent store coordinator.

这很容易做到,但是,当涉及到保存它在文档中说的上下文,它是错误容易这样做。所以我的问题如下:

That's easily done, however, when it comes to saving the context it says in the documentation it is error prone to do so. So my question is the following:


  1. 如果我在NSOperationQueue中运行不同的操作,那么这些操作会互相干扰保存管理对象上下文?

  1. If I have different operations running in the NSOperationQueue, could those operations interfere with each other while saving the managed object context? Or does it wait to execute the following operation till the saving has been complete?

我可以在NSOperation中安全地保存上下文吗?

Can I safely save the context in a NSOperation? Or is it really bad practice?

我希望有人能为这件事发光,因为我真的很坚持

I hope someone can shine a light on this matter, because I am really stuck at the moment.

推荐答案

您需要做的是以下操作:

What you need to do is the following:


  1. 为每个NSOperation创建一个受管对象上下文。 在主方法上创建此新上下文。

  2. 分配上下文持久存储协调器。

  3. 创建观察器以接收NSManagedObjectContextDidSaveNotification 。这是当NSOperation的上下文发生变化时主要上下文知道的唯一方式。 确保合并调用是在线程/阻止合并上下文存在中进行的。如果要合并主线程的上下文,请使用NSOperation上下文中的通知调用主线程上的mergeChangesFromContextDidSaveNotification:方法。

  1. Create a managed object context for each NSOperation. Create this new context on the main method, because this is when it's executing on the right thread.
  2. Assign the context persistent store coordinator.
  3. Create an observer to receive the NSManagedObjectContextDidSaveNotification. This is the only way the main context will know at the time the changes were made on the NSOperation's context. Make sure the merge call is made on the thread/block the merging context lives in. If you are merging with the main thread's context, call the mergeChangesFromContextDidSaveNotification: method on the main thread with the notification from the NSOperation's context.

另外,询问自己是否真的想让所有这些操作同时工作。根据文档:

Also, ask yourself if you really want to have all these operations working concurrently. Per the documentation:


默认最大操作数由NSOperationQueue对象根据当前系统条件动态确定。

The default maximum number of operations is determined dynamically by the NSOperationQueue object based on current system conditions.

您无法控制多少个NSOperations将同时运行。如果这不是你想要的,你可能更好,如果你只是一个序列NSOperationQueue(maxConcurrentOperation = 1),考虑到你将锁定数据库做保存这一事实,还因为你有网络

You do not have control over how many NSOperations will be operating at the same time. If this is not what you want, you might be better if you just go with a serial NSOperationQueue (maxConcurrentOperation=1), considering the fact that you are going to be locking the database to do the save, and also because you have networking being done as well.

如果您采取上述预防措施,您可以安全地保存在NSOperation的主要方法中。

You can safely save inside the NSOperation's main method, if you take the precautions mentioned above.

这篇关于什么是线程安全的方式来保存上下文形式核心数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆