核心数据:父上下文阻止子级 [英] Core Data: parent context blocks child

查看:147
本文介绍了核心数据:父上下文阻止子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用核心数据在应用中进行一些后台处理。后台处理在子managedObjectContext上完成。上下文初始化:

I'm doing some background processing in an app with core data. The background processing is done on a child managedObjectContext. Context initialization:

appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

// the moc in appDelegate is created with .MainQueueConcurrencyType
mainThreadMOC = appDelegate.managedObjectContext!
backgroundMOC = NSManagedObjectContext(concurrencyType:NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)
backgroundMOC?.parentContext = mainThreadMOC

后台处理按以下方法完成:

Background processing is done in the following method:

// download all new transaction log entries
func syncItems() {

... set up the query object for parse

let moc = CoreDataStore.sharedInstance.backgroundMOC

// perform download
moc?.performBlock( {
    self.runQuery(query)   // Download stuff und do some core data work
    })
}

调试器显示块内的所有工作确实都在后台线程中。

The debugger shows that all work inside the block is indeed in a background thread.

当我从主线程调用此函数并立即使用冗长的核心数据操作阻止主线程(用于测试目的)时,我看到后台线程停止并且仅在主线程空闲时继续执行。

When I call this function from the main thread and immediately block the main thread (for test purpose) with a lengthy core data operation, I see that the background thread stops and only continues execution when the main thread is idle.

// this is called from a view controller in the main thread

syncItems() // should start to work in background
for i in 0...200 {
    // do some core data work in main thread
}
// syncItems starts to work after the blocking for-loop ends.

为什么会这样?

推荐答案

不要使用父子上下文设置。

父子上下文不是很好几乎任何事情的方法。只需使用一个简单的堆栈:两个上下文与一个共享持久存储协调器。

Parent-child context are not a good approach for just about anything. Just use a simple stack: two contexts with one shared persistent store coordinator.

父子上下文只会增加很多混乱,而不会给你太多任何东西。这是一个很容易被误解的概念。我希望像mzarra这样的人会停止提倡这种设置。这对社区是一种伤害。

Parent-child contexts just add a lot of confusion without giving you much of anything. It’s a pretty misunderstood concept. I wish people like mzarra would f***ing stop advocating that setup. It’s a disservice to the community.

如果您的背景上下文是主要上下文的子上下文,则必须锁定两者主要和背景上下文,每当背景上下文需要保存。这会阻止UI。并且您必须第二次锁定UI以将这些更改从UI传播到PSC。如果使用后台上下文,则必须将更改合并到主上下文中,但是您只能为当前在该上下文中注册的对象执行操作。如果您添加了新对象,或者当前未注册(引用)的更新/删除对象,那么基本上是无操作。

If your background context is a child context of your main context, you will have to lock both the main and the background context, whenever the background context needs to save. This blocks the UI. And you’ll have to lock the UI a 2nd time to propagate those changes from the UI into the PSC. If you use a background context, you will have to merge changes into the main context, but you'll only do work for those objects that are currently registered with that context. If you added new objects, or updated / deleted objects that are not currently registered (referenced) that’s basically a no-op.

这篇关于核心数据:父上下文阻止子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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