SwiftUI holding reference to deleted core data object causing crash

后端 未结 6 640
谎友^
谎友^ 2021-01-31 04:38

Im finding it impossible to use core data with SwiftUI, because as I pass a core data to a view observed object variable, the navigation link view will hold a reference to the o

6条回答
  •  太阳男子
    2021-01-31 04:53

    I have tried all previous solutions, none worked for me.

    This one, worked.

    I had my list like this:

    List {
      ForEach(filteredItems, id: \.self) { item in
        ListItem(item:item)
      }
    .onDelete(perform: deleteItems)
    
    
    private func deleteItems(offsets: IndexSet) {
      //deleting items
    

    This was crashing.

    I modified the code to this one

    List {
      ForEach(filteredItems, id: \.self) { item in
        ListItem(item:item)
      }
      .onDelete { offsets in
         // delete objects
      }
    

    This works fine without crashing.

    For heaven's sake, Apple!

提交回复
热议问题