Avatar
@swift-5.7.3 @swift-5.6.3 import Foundation import CoreData @objc(Test) class Test: NSManagedObject { } extension Test { @NSManaged public var text: String } class Model: ObservableObject { @MainActor @Published var list: [Test] = [] init() { let container = NSPersistentContainer(name: "") NotificationCenter.default.addObserver( forName: NSManagedObjectContext.didChangeObjectsNotification, object: container.persistentStoreCoordinator, queue: nil ) { notification in if let updatedList = notification.userInfo?[NSUpdatedObjectsKey] as? Set<NSManagedObject> { for updated in updatedList { Task { if let i = await self.list.firstIndex(where: { $0.objectID == updated.objectID }) { await MainActor.run(body: { self.list[i].text = "Changed" }) } } } } } } }