1 year ago

#360620

test-img

user2924482

SwiftUI/Combine: Observe NSManagedObject entity

I have a question regarding how can observe am NSManageObject entity. For example I have this two CoreData entities(Todo and Done):

enter image description here

But I want to observe when the done entity has change. Currently I have the following:

class ViewModel: ObservableObject {
    @Published internal var todo:[Todo] = []
    private var cancellables = Set<AnyCancellable>()
    private var context: NSManagedObjectContext
    private let persistanceContainer = PersistanceController.share
    init() {
        self.context = persistanceContainer.container.viewContext
        NotificationCenter.default.publisher(for: .NSManagedObjectContextObjectsDidChange)
            .sink { _ in
                print("sink")
            } receiveValue: { _ in
                print("new value")
            }.store(in: &cancellables)
    }
}

The problem with this implementation is I'm getting a notification every time either entity has change. Any of you knows how I specify a particular entity in this case Done entity?

xcode

core-data

swiftui

combine

nsmanagedobjectcontext

0 Answers

Your Answer

Accepted video resources