3

I have two pages in SwiftUI which can be switched using tabView, one is ProjectPage the other is HabitPage. In the ProjectPage, a list of projects can be displayed. In the HabitPage, a list of habits can be displayed. New Habit can be added in this page as well. All of projects and habits will be stored in the core data. A project has a relation with a task, a task has a relation with a project. After I create a new habit and I switch to the Project page, the program will crash.

Output

2020-01-14 14:03:51.156969+1100 Demo9R[35096:12471800] precondition failure: attribute failed to set an initial value: 74

part of my code:

struct ProjectView: View {
@Environment(\.managedObjectContext) var managedObjectContext

@FetchRequest(entity: Project.entity(), sortDescriptors: []) var projects: FetchedResults<Project>

@State var popAddProject: Bool = false

var body: some View {
    ZStack {
        NavigationView {
            List {
                ForEach(projects, id: \.self) { project in
                    NavigationLink(destination: ProjectProfile(project: project).environment(\.managedObjectContext, self.managedObjectContext)) {
                        ProjectCard(name: project.wrappedName, tasks: project.tasksName)
                    }
                }
            }
            .navigationBarTitle("Project")
        }

        PlusButton() {
            self.popAddProject.toggle()
        }

        if self.popAddProject {
            MiddleLayer(ifDisapper: self.$popAddProject)

            AddProjectView(ifAppear: self.$popAddProject)
                .background(Color.white)
                .shadow(radius: 16)
                .cornerRadius(12.0)
                .padding()
                .environment(\.managedObjectContext, managedObjectContext)

        }
    }
}

}

HabitPage:

struct HabitView: View {
@Environment(\.managedObjectContext) var managedObjectContext

@FetchRequest(entity: Habit.entity(), sortDescriptors: []) var habits: FetchedResults<Habit>
@FetchRequest(entity: Task.entity(), sortDescriptors: []) var tasks: FetchedResults<Task>

@State var popAddHabit = false

var body: some View {
    ZStack {
        NavigationView {
            List {
                ForEach(self.habits, id: \.self) { habit in
                    HabitCard(habit: habit)
                }
            }
            .navigationBarTitle("Habits")
        }

        PlusButton {
            self.popAddHabit.toggle()
        }
        .disabled(self.tasks.count == 0)

        if self.popAddHabit {
            MiddleLayer(ifDisapper: self.$popAddHabit)

            AddHabitView(ifAppear: self.$popAddHabit)
        }
    }
}

}

ContentView

struct ContentView: View {
@State private var selection = 0

var body: some View {
    TabView(selection: $selection){
        ProjectView()
            .font(.title)
            .tabItem {
                VStack {
                    Image("first")
                    Text("Project")
                }
            }
            .tag(2)
        HabitView()
            .font(.title)
            .tabItem {
                VStack {
                    Image("second")
                    Text("Second")
                }
            }
            .tag(1)
        TodayView()
            .font(.title)
            .tabItem {
                VStack {
                    Text("Third")
                }
            }
            .tag(0)
    }
    .edgesIgnoringSafeArea(.top)
}

}

Could anyone tell me why? I have tried for a long time

buibuibui404
  • 87
  • 1
  • 8

0 Answers0