3

I'm trying to find a simple way to use CoreData with combine.

In short, I've been using combine really successfully with plain old Swift objects that are ObservableObjects. I can build pipelines off @Published properties, and I can do all the swift-y and combine-y things with them.

Now I want to do the same with CoreData objects.

I've run into the following:

  1. @NSManaged properties cannot be declared @Published. That's a bummer.
  2. I read about ObjectWillChange publisher on NSManagedObject but I can't find an example that's meaningful - all examples I can find do a .sink { print }. I wanted to be able to discover what property changed, whether it changed in the background (due to an iCloud sync) or due to a user interaction, etc.
  3. I read about a technique which replaces all @NSManaged properties with Swift properties and then being able to leverage property observers - didSet and didGet. This doesn't seem great as it's not scalable (changing every object by hand ...). It also seems like a hack and I'm afraid of places where it would fall over. Furthermore, it doesn't seem like you can use @Published in this way. So I would potentially need to have this core data property handle didSet and write to another property that is @Published for the sole purpose of using combine ... seems like a hack.
  4. I can do the old school NSNotificationCenter but that just seems unswift-y and uncombine-y.

Has anyone gotten this to work in the way that it probably should work - that is, combine on CoreData objects is as natural as it is on plain old swift objects?

tng
  • 4,286
  • 5
  • 21
  • 30
  • 1
    Does this answer your question? [How to: Using Combine to react to CoreData changes in the background](https://stackoverflow.com/questions/60495494/how-to-using-combine-to-react-to-coredata-changes-in-the-background) – Joakim Danielson Jul 05 '20 at 20:16

0 Answers0