Why does @Published not work in NSManagedObject?

@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 answer

  • answered 2022-05-04 11:24 John

    I managed to fix it like this:

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

    Since the change notification is sent manually, @Published seems to be optional.

    No idea why this is needed, it should be automatic. Is that not the whole point of @Published?

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum