Problem with CoreData + Picker/DatePicker

Hello everyone,

I'm having trouble with a datePicker and a picker. I believe that the problems are caused by the use of coreData because I have tried my pickers with normal values and it works perfectly.

The project is a ToDoList App, both picker are used in the detail view of a task to present and modify the time that the task take (picker) and the due date for that task (datePicker).

The first problem is with the picker. The picker is scrollable but it always return to the first index which is 0 (forEach loop 0 to 59 for min). Although I know that the value is correctly saved in the coreData database because I use it in an other place.

The second problem is with the datePicker. The datePicker doesn't work well. To be able to select a date, I have to do a long press instead of a normal press.

You can take a look at the views here with the corresponding code




struct DurationView: View {

    

    @ObservedObject var taskEntity : TaskEntity

    @ObservedObject var toDoListEntity : ToDoListEntity

    

    

    var body: some View {

        VStack {

            HStack {

                Text("Durée")

                    .font(.system(.title))

                    .fontWeight(.regular)

                Spacer()

                Button(action: {

                    taskEntity.durationHour = 0

                    taskEntity.durationMinutes = 0

                }, label: {

                    if taskEntity.durationHour == 0 && taskEntity.durationMinutes == 0 {

                        ZStack {

                            RoundedRectangle(cornerRadius: 5, style: .continuous)

                                .foregroundColor(.blue)

                                .frame(width: 130, height: 30)

                            

                            Text("Pas de durée")

                                .padding(5)

                                .font(.system(size: 20))

                                .foregroundColor(.white)

                        }

                    } else {

                        ZStack {

                            Text("Pas de durée")

                                .padding(5)

                                .font(.system(size: 20))

                            

                            RoundedRectangle(cornerRadius: 5, style: .continuous)

                                .stroke(lineWidth: 1)

                                .foregroundColor(.blue)

                                .frame(width: 130, height: 30 )

                        }

                    }

                })

            }

            

            HStack {

                Picker("heure", selection: $taskEntity.durationHour) {

                    ForEach(0..<24) {

                        Text("\($0)")

                    }

                }.pickerStyle(WheelPickerStyle())

                

                Text("h")

                

                Picker("minute", selection: $taskEntity.durationMinutes) {

                    ForEach(0..<60) {

                        Text("\($0)")

                    }

                }.pickerStyle(WheelPickerStyle())

                

                Text("min")

            }

        }

    }

}




struct DateView: View {

    

    @ObservedObject var taskEntity : TaskEntity

    

    var body: some View {

        VStack {

            HStack {

                Text("Date")

                    .font(.system(.title))

                    .fontWeight(.regular)

                

                Spacer()

                

                Button(action: {

                    taskEntity.dateSet.toggle()

                }, label: {

                    if !taskEntity.dateSet  {

                    ZStack{

                        RoundedRectangle(cornerRadius: 5, style: .continuous)

                            .foregroundColor(.blue)

                            .frame(width: 160, height: 30)

                        

                        Text("Ajouter une date")

                            .padding(5)

                            .font(.system(size: 20))

                            .foregroundColor(.white)

                    }

                } else {

                    ZStack{

                        RoundedRectangle(cornerRadius: 5, style: .continuous)

                            .stroke(lineWidth: 1)

                            .foregroundColor(.blue)

                            .frame(width: 120, height: 30)

                        

                        Text("Pas de date")

                            .padding(5)

                            .font(.system(size: 20))

                    }

                }})

            }

            

            if taskEntity.dateSet {

                DatePicker("Date pour la tâche", selection: $taskEntity.date, in: Date()...,displayedComponents: .date)

                        .datePickerStyle(.graphical)

                        

                

                HourView(taskEntity: taskEntity)

            }

        }

    }

}

if you have an idea I'll be glad to hear it.

Thank you for your time

Problem with CoreData &#43; Picker/DatePicker
 
 
Q