'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()
}
}
Solution 1:[1]
Here might be some useful links for you to prevent this kind of situations:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JAY_Panchal |
