意外的视图重新加载SwiftUI

时间:2019-11-27 21:38:01

标签: swift view swiftui reload init

问题

我正在使用Swiftui开发健身应用程序。

问题是我的一个View意外重新加载,因此调用了View的OpenCV_Found。 您可以说我为什么会出问题?因为视图又是init()的事实,所以我的init()类也是初始化的。

视图结构:

___ [TrainingSessionMain] 再次初始化的主视图

______ [TrainingSessionMainTraining]

_________ [TrainingSessionMainTrainingBoard]-> 查看调用导致重新加载的函数的

_________ [....]

代码

TrainingSessionMain

TrainingCore

TrainingSessionMainTraining

struct TrainingSessionMain: View {

    ....

    @ObservedObject private var trainingCore : TrainingCore

    @ObservedObject var myTrainingSession : TrainingSession

    ....

    /***
    It's this init that is call again
   ***/
    init(newTrainingSession : TrainingSession) {
        self.myTrainingSession = newTrainingSession

        self.trainingCore = TrainingCore()
}

TrainingSessionMainTrainingBoard

struct TrainingSessionMainTraining: View {
    ....

    @ObservedObject var trainingCore : TrainingCore

    var body: some View {
      ....
    }

TrainingCore

struct TrainingSessionMainTrainingBoard: View {
    ....

    @ObservedObject var trainingCore : TrainingCore

    var body: some View {
                               LabeledButton(closureAction: {
                                if self.trainingCore.trainingState == .notInit {
                                    self.trainingCore.setTrainingSession(self.myTrainingSession)
                                                                                                           self.trainingCore.setTrainingExercice(self.myTrainingExercice)
                                    self.trainingCore.setTrainingSessionExercices(self.myTrainigSessionExercice)
                                }
                                /** this function call the function in TrainingCore that cause reload**/
                                self.trainingCore.buttonOneClic()

                            }, mainColor: Color(Constants.UI.Colors.mainLight), secondColor: .white, text: self.trainingCore.buttonOneName, big: false, fontSize: 20)
                                .padding([.trailing], 5)
      ....
    }

所以 final class TrainingCore : ObservableObject { var managedObjectContext : NSManagedObjectContext? ... public func saveHistory(){ if self.trainingExercices != nil && self.managedObjectContext != nil{ print("enter in saveHistory()") /** * Creating TrainingSessionHistoryObject */ let newTrainingSessionHistory = TrainingSessionHistory(context: self.managedObjectContext!) /** if theses lines are commented -> the view doesn't reload -> so the problem come from here**/ newTrainingSessionHistory.trainingSession = self.trainingSession! self.trainingSession!.addToTrainingSessionHistories(newTrainingSessionHistory) } } newTrainingSessionHistory.trainingSession = self.trainingSession导致self.trainingSession!.addToTrainingSessionHistories(newTrainingSessionHistory)的刷新,重置了TrainingSessionMain的状态

重要事实

我无法解释的事实是,第一次调用TrainingSessionMain.trainingCore:视图重新加载,但第二次视图未重新加载,一切都发生了。

0 个答案:

没有答案