MacOs sheet not opening and application stuck

Dear all, I'm developing an application with SwiftData for Mac and since the beginning I'm experiencing issues in opening sheets.

In particular, I have a view where I'm trying to open a sheet which is called TrainingAttendanceView, but it not working at all. When I click on it, the application is stuck and not working anymore. Debugger is not retrieving anything and I don't have any clue on what to do.

Could you please let me know what I'm doing wrong or if there is any possible workaround?

Thanks in advance for the support, A.

Here the code of the view where I'm calling the sheet:

 import SwiftUI
import SwiftData

struct CalendarView: View {
    @Environment(\.modelContext) private var context
    @ObservedObject var season: Season

    @Query var trainings: [Training]

    @State private var isAddingTraining = false
    @State private var trainingToEdit: Training?
    @State private var trainingToDelete: Training?
    @State private var trainingForAttendance: Training?
    @State private var showAlert = false
    @State private var isShowingAttendance = false

    var body: some View {
        VStack {
            ScrollView {
                VStack(alignment: .leading, spacing: 12) {
                    ForEach(months, id: \.self) { month in
                        monthView(for: month)
                    }
                }
                .padding()
            }
            .alert(isPresented: $showAlert) {
                Alert(
                    title: Text("Conferma cancellazione"),
                    message: Text("Sei sicuro di voler cancellare questo allenamento?"),
                    primaryButton: .destructive(Text("Elimina")) {
                        if let training = trainingToDelete {
                            deleteTraining(training)
                        }
                    },
                    secondaryButton: .cancel {
                        trainingToDelete = nil
                    }
                )
            }
            .sheet(isPresented: $isAddingTraining) {
                if let trainingToEdit = trainingToEdit {
                    EditTrainingView(isEditingTraining: $isAddingTraining, season: season, training: trainingToEdit)
                } else {
                    AddTrainingView(isAddingTraining: $isAddingTraining, season: season)
                }
            }
            .sheet(isPresented: $isShowingAttendance) {
                if let training = trainingForAttendance {
                    TrainingAttendanceView(season: season, trainingDate: training.date, training: training)
                }
            }
            .toolbar {
                ToolbarItem(placement: .confirmationAction) {
                    Button(action: {
                        isAddingTraining = true
                        trainingToEdit = nil
                    }) {
                        Text("+ Add Training")
                            .foregroundColor(.blue)
                    }
                }
            }
        }
    }

    private var trainingsByMonth: [String: [Training]] {
       ...
    }

    private var months: [String] {
        ...
    }

    private func monthIndex(for monthString: String) -> String? {
        ...
    }

    private func deleteTraining(_ training: Training) {
       ...
    }

    private func monthView(for month: String) -> some View {
        VStack(alignment: .leading, spacing: 8) {
            Text(month)
                .font(.headline)
                .padding(8)
                .background(Color.clear)

            if let monthIndex = monthIndex(for: month), let trainingsInMonth = trainingsByMonth[monthIndex] {
                if trainingsInMonth.isEmpty {
                    Text("Non ci sono eventi per questo mese")
                        .padding(8)
                } else {
                    ForEach(trainingsInMonth, id: \.self) { training in
                        HStack(alignment: .center, spacing: 8) {
                            Rectangle()
                                .fill(Color.green)
                                .frame(width: 4)

                            VStack(alignment: .leading, spacing: 4) {
                                Text("Allenamento")
                                    .font(.subheadline)
                                    .bold()

                                Text(training.date, style: .date)
                                    .font(.footnote)

                                Text("Ore: \(training.startTime, style: .time) - \(training.endTime, style: .time)")
                                    .font(.footnote)
                            }

                            Spacer()

                            HStack(spacing: 12) {
                                Button(action: {
                                    trainingToEdit = training
                                    isAddingTraining = true
                                }) {
                                    Image(systemName: "pencil")
                                }
                                .buttonStyle(BorderlessButtonStyle())
                                .foregroundColor(.blue)

                                Button(action: {
                                    trainingForAttendance = training
                                    isShowingAttendance = true
                                }) {
                                    Image(systemName: "person.3.fill")
                                }
                                .buttonStyle(BorderlessButtonStyle())
                                .foregroundColor(.blue)

                                Button(action: {
                                    trainingToDelete = training
                                    showAlert = true
                                }) {
                                    Image(systemName: "trash")
                                        .foregroundColor(.red)
                                }
                                .buttonStyle(BorderlessButtonStyle())
                            }
                            .frame(width: 100, alignment: .center)
                        }
                    }
                }
            } else {
                Text("Non ci sono eventi per questo mese")
                    .padding(8)
            }
        }
        .padding()
    }
}

While here the starting code of the TrainingAttendanceView:

import SwiftData

struct TrainingAttendanceView: View {
    @Environment(\.modelContext) private var context
    @ObservedObject var season: Season
    var trainingDate: Date
    @ObservedObject var training: Training

    @State private var attendance: [PlayerAttendance] = []

    @Query private var players: [Player]

    init(season: Season, trainingDate: Date, training: Training) {
        self.season = season
        self.trainingDate = trainingDate
        self.training = training
...
Answered by newwbee in 799846022

Try to replace TrainingAttendanceView with a simple view (example: Color.red) and see what happens.

If the sheet opens then the problem could be with TrainingAttendanceView, begin commenting out code till you isolate the problem.

If the sheet doesn't open with a simple view then the problem could be in the parent view presenting it.

After isolating the problem create a project with minimum code to reproduce the issue and then post it in the forum so that others can have a look.

Accepted Answer

Try to replace TrainingAttendanceView with a simple view (example: Color.red) and see what happens.

If the sheet opens then the problem could be with TrainingAttendanceView, begin commenting out code till you isolate the problem.

If the sheet doesn't open with a simple view then the problem could be in the parent view presenting it.

After isolating the problem create a project with minimum code to reproduce the issue and then post it in the forum so that others can have a look.

MacOs sheet not opening and application stuck
 
 
Q