SwiftUI holding reference to deleted core data object causing crash

后端 未结 6 619
谎友^
谎友^ 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 05:04

    A view modifier for this (based on conditional view modifiers):

    import SwiftUI
    import CoreData
    
    extension View {
        @ViewBuilder
        func `if`(
            _ condition: Bool,
            transform: (Self) -> Transform
        ) -> some View {
            if condition {
                transform(self)
            } else {
                self
            }
        }
    }
    
    extension View {
        func hidingFaults(_ object: NSManagedObject) -> some View {
            self.if(object.isFault) { _ in EmptyView() }
        }
    }
    

    Having said that, it's worth checking you're performing CoreData operations asynchronously on the main thread, doing it synchronously can be a source of grief (sometimes, but not always).

提交回复
热议问题