SwiftUI NSManagedObject的更改不会更新视图

在我的视图模型中,如果更新了NSManagedObject属性,则视图将不再更新。我已经附上了代码和视图模型。

我在该行的前面添加了一条注释,以中断视图更新。

class StudySessionCounterViewModel: ObservableObject {

    fileprivate var studySession: StudySession

    init(_ studySession: StudySession) {
        self.studySession = studySession
    }

    @Published var elapsedTime = 14 * 60
    @Published var circleProgress: Double = 0

    var timer: Timer?

    var formattedTime: String {
        get {
            let timeToFormat = (Int(studySession.studyTime) * 60) - elapsedTime
            let minutes = timeToFormat / 60 % 60
            let seconds = timeToFormat % 60
            return String(format:"%02i:%02i",minutes,seconds)
        }
    }

    func startTimer() {
        self.timer = Timer.scheduledTimer(timeInterval: 1,target: self,selector: #selector(self.timerTicked),userInfo: nil,repeats: true)
        studySession.isactive = true //Adding this stops my view from updating

    }

    @objc func timerTicked() {
        elapsedTime += 1
        circleProgress = (Double(elapsedTime) / Double(studySession.studyTime * 60))
    }

    func stop() {
        timer?.invalidate()
    }

}

这是使用该视图模型的视图。添加该行时,代表格式化时间的文本不再更改,并且进度圈的进度保持不变。 如果我删除该行,一切都会更新并按预期工作。

struct StudySessionCounterView: View {

    @Environment(\.presentationmode) var presentationmode

    @ObservedObject var studySessionCounterVM: StudySessionCounterViewModel

    var studySession: StudySession

    init(_ studySession: StudySession) {
        studySessionCounterVM = StudySessionCounterViewModel(studySession)
        self.studySession = studySession
    }

    @State var showAlert = false
    @State var isCounting = false

    var body: some View {

        VStack {
            ZStack {
                Text(studySessionCounterVM.formattedTime)
                    .font(.largeTitle)
                ProgressRingView(size: .large,progress: $studySessionCounterVM.circleProgress)
            }

            Spacer()

            if isCounting {
                Button(action: {
                    self.studySessionCounterVM.stop()
                    self.isCounting = false
                }) {
                    Image(systemName: "stop.circle")
                        .resizable()
                        .frame(width: 64,height: 64,alignment: .center)
                        .foregroundColor(.orange)
                }
            } else {
                Button(action: {
                    self.studySessionCounterVM.startTimer()
                    self.isCounting = true
                }) {
                    Image(systemName: "play.circle")
                        .resizable()
                        .frame(width: 64,alignment: .center)
                        .foregroundColor(.orange)
                }
            }

        }.padding()
            .navigationBarTitle("Matematica",displayMode: .inline)
    }
}

更新:发现每次NSManagedObject更改属性时,都会重新初始化视图模型。不幸的是,仍然没有解决办法

q775008 回答:SwiftUI NSManagedObject的更改不会更新视图

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3101299.html

大家都在问