How to: Using Combine to react to CoreData changes in the background

后端 未结 3 1009
梦谈多话
梦谈多话 2020-12-30 11:26

I want to achieve the following: Whenever someone triggers a CoreData save (ie. NSManagedObjectContextDidSave notification gets sent), I\'d like to perform some

3条回答
  •  离开以前
    2020-12-30 12:04

    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.

提交回复
热议问题