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:
@NSManaged
properties cannot be declared@Published
. That's a bummer.- I read about
ObjectWillChange
publisher onNSManagedObject
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. - I read about a technique which replaces all
@NSManaged
properties with Swift properties and then being able to leverage property observers -didSet
anddidGet
. 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 handledidSet
and write to another property that is@Published
for the sole purpose of using combine ... seems like a hack. - 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?