Why does @Published not work in NSManagedObject?

429 views Asked by At

@Published publishes correctly when used in an ObservableObject. But does not seem to publish when used in an NSManagedObject (which conforms to ObservableObject).

In the following example when using Data1 the text below the picker is updated. But using Data2 it is not. Why is that?

import SwiftUI
import CoreData

class Data1: ObservableObject {
    @Published var direction: Direction = .left
}

class Data2: NSManagedObject { // NSManagedObject conforms to ObservableObject
    @Published var direction: Direction = .left
}

enum Direction {
    case left
    case right
}

@main
struct SwiftUITestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView().environmentObject(Data2()) // Or Data1() as needed
        }
    }
}

struct ContentView: View {
    @EnvironmentObject private var data: Data2
    
    var body: some View {
        VStack {
            Picker("", selection: $data.direction) {
                Text("Left").tag(Direction.left)
                Text("Right").tag(Direction.right)
            }.pickerStyle(.wheel)
            switch data.direction {
            case .left:
                Text("Selected: Left")
            case .right:
                Text("Selected: Right")
            }
        }.padding()
    }
}
1

There are 1 answers

0
John On BEST ANSWER

The solution is to add a transient Core Data attribute. Transient attributes are published but not persisted.

Alternatively, you can publish changes like this:

class Data2: NSManagedObject {
    @Published var direction: Direction = .left { // @Published is optional
        willSet {
            objectWillChange.send()
        }
    }
}