0

I have a SwiftUI App which have a MainView and this is calling a sheet like

this:.sheet(isPresented: $showingSheetFilter) {
    FilterView()
}

The FilterView looks like this:

import SwiftUI

struct FilterView: View {

    @FetchRequest(
        entity: Category.entity(),
        sortDescriptors: [
            NSSortDescriptor(keyPath: \Category.title, ascending: true)
        ]

    ) var categories: FetchedResults<Category>

    var body: some View {
        List {
            ForEach(categories, id: \.self) { (cat: Category) in
                Text(cat.title!)
            }
        }.onAppear {
            print(self.categories.count)
        }
    }
}

If I call the sheet I get an Dump:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

In the onAppear. If I remove the onAppear I get it in the ForEach.

Why this is happening?

gurehbgui
  • 14,236
  • 32
  • 106
  • 178

1 Answers1

0

As I commented to question at my environment all works fine. The only difference I see is that I pass context in my view-in-sheet explicitly as below

Button("Test Sheet") { self.isSheet = true }
    .sheet(isPresented: $isSheet) {
        TestListView().environment(\.managedObjectContext, context)
}

Please try to do the same. Just in case.

Asperi
  • 228,894
  • 20
  • 464
  • 690