如何:使用Combine在后台对CoreData更改做出反应 [英] How to: Using Combine to react to CoreData changes in the background

查看:65
本文介绍了如何:使用Combine在后台对CoreData更改做出反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现以下目标:每当有人触发CoreData保存(即,发送 NSManagedObjectContextDidSave 通知)时,我想执行一些 background 计算基于更改后的NSManagedObject。具体示例:假设在一个笔记应用程序中,我想异步计算所有笔记中的单词总数。

I want to achieve the following: Whenever someone triggers a CoreData save (ie. NSManagedObjectContextDidSave notification gets sent), I'd like to perform some background calculation based the changed NSManagedObject. Concrete example: Assume in a notes app, I want to asynchronously calculate the total number of words in all notes.

当前的问题在于,NSManagedObject上下文是显式的绑定到线程,不鼓励您在此线程外使用 NSManagedObject

The problem currently lies with the fact that NSManagedObject context is explicitly bound to thread and you are discouraged from using NSManagedObjects outside this thread.

我已经设置了两个<$ c我的 SceneDelegate 中的$ c> NSManagedObjectContext :

I have setup two NSManagedObjectContexts in my SceneDelegate:

let context =(UIApplication.shared.delegate as!AppDelegate).persistentContainer.viewContext
let backgroundContext =(UIApplication.shared.delegate as!AppDelegate).persistentContainer.newBackgroundContext()

我还通过 NotificationCenter.default.publisher(for:.NSManagedObjectContextDidSave)订阅了通知,并且正在接收保存通知我只触发一个 managedObjectContext.save()后,两次。但是,两个通知都是从同一线程(即UIThread)发送的,并且用户词典中的所有 NSManagedObjects 都具有 .managedObjectContext viewContext 而不是 backgroundContext

I also have subscribed to the notification via NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave) and am receiving a save notification twice after I trigger only one managedObjectContext.save(). However, both notifications are sent from the same thread (which is the UIThread) and all NSManagedObjects in the user dictionary have a .managedObjectContext which is the viewContext and not the backgroundContext.

我的想法是根据关联的 NSManagedObjectContext 是否作为背景过滤通知,因为我认为通知也通过(专用)DispatchQueue发送但是似乎所有通知都是在UIThread上发送的,并且从未使用过后台上下文。

My idea was to filter the notifications based on whether or not the associated NSManagedObjectContext was the background one as I assumed that the notification is also sent on the (private) DispatchQueue but it seems all notifications are sent on the UIThread and the background context is never used.

有任何解决方法的想法吗?这是一个错误吗?如何基于 backgroundContext 检索通知,并在关联的DispatchQueue上运行下游任务?

Any idea on how to solve this? Is this a bug? How can I retrieve notifications based on the backgroundContext with downstream tasks being run on the associated DispatchQueue?

推荐答案

您可以将要观察的对象传递给 publisher(for:)

You can pass the object you want to observe to publisher(for:):

NotificationCenter.default
  .publisher(for: .NSManagedObjectContextDidSave, object: backgroundMoc)
  .sink(receiveValue: { notification in
    // handle changes
  })

这只会监听与后台托管对象上下文有关的通知可以安全地对该上下文的队列进行处理。

That will only listen for notifications related to a background managed object context which means you can do processing on that context's queue safely.

这篇关于如何:使用Combine在后台对CoreData更改做出反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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