Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

ios - SwiftUI持有对导致崩溃的已删除核心数据对象的引用(SwiftUI holding reference to deleted core data object causing crash)

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 object even after the view has disappeared, so as soon as I delete the object from context the app crashes, with no error messages.

(我发现无法在SwiftUI中使用核心数据,因为当我将核心数据传递给观察到的对象变量时,即使该视图消失了,导航链接视图也将保留对对象的引用,因此我删除了应用程序从上下文中崩溃对象,没有错误消息。)

I have confirmed this by wrapping the core data object variable into a view model as an optional, then set the object to nil right after the context delete action and the app works fine, but this is not a solution because I need the core data object to bind to the swift ui views and be the source of truth.

(我已经通过将核心数据对象变量包装到视图模型中作为可选变量来确认了这一点,然后在上下文删除操作和应用正常运行之后立即将对象设置为nil,但这不是解决方案,因为我需要核心数据对象绑定到快速的ui视图并成为真理的源头。)

How is this suppose to work?

(这应该如何工作?)

I seriously cannot make anything remotely complex with SwiftUI it seems.

(我似乎无法用SwiftUI使任何事情变得遥不可及。)

I have tried assigning the passed in core data object to a optional @State, but this does not work.

(我尝试将传入的核心数据对象分配给可选的@State,但这不起作用。)

I cannot use @Binding because it's a fetched object.

(我不能使用@Binding,因为它是一个获取的对象。)

And I cannot use a variable, as swiftui controls require bindings.

(而且我不能使用变量,因为swiftui控件需要绑定。)

It only makes sense to use a @ObservedObject, but this cannot be an optional, which means when the object assigned to it gets deleted, the app crashes, because i cannot set it to nil.

(仅使用@ObservedObject是有意义的,但这不是可选的,这意味着当分配给它的对象被删除时,应用程序崩溃,因为我无法将其设置为nil。)

Here is the core data object, which is an observable object by default:

(这是核心数据对象,默认情况下是可观察的对象:)

class Entry: NSManagedObject, Identifiable {

    @NSManaged public var date: Date
}

Here is a view that passes a core data entry object to another view.

(这是将核心数据输入对象传递到另一个视图的视图。)

struct JournalView: View {

    @Environment(.managedObjectContext) private var context

    @FetchRequest(
        entity: Entry.entity(),
        sortDescriptors: [],
        predicate: nil,
        animation: .default
    ) var entries: FetchedResults<Entry>

    var body: some View {
        NavigationView {
            List {
                ForEach(entries.indices) { index in
                    NavigationLink(destination: EntryView(entry: self.entries[index])) {
                        Text("Entry")
                    }
                }.onDelete { indexSet in
                    for index in indexSet {
                        self.context.delete(self.entries[index])
                    }
                }
            }
        }
    }
}

Now here is the view that accesses all the attributes from the core data entry object that was passed in. Once, I delete this entry, from any view by the way, it is still referenced here and causes the app to crash immediately.

(现在,这里是从传入的核心数据条目对象访问所有属性的视图。一次,我从任何视图中删除了该条目,顺便说一句,这里仍然引用该条目,并导致应用程序立即崩溃。)

I believe this also has something to do with the Navigation Link initializing all destination view before they are even accessed.

(我相信这也与导航链接在访问所有目标视图之前进行初始化有关。)

Which makes no sense why it would do that.

(为什么这样做会没有意义。)

Is this a bug, or is there a better way to achieve this?

(这是一个错误,还是有更好的方法来实现?)

I have even tried doing the delete onDisappear with no success.

(我什至尝试删除onDisappear都没有成功。)

Even if I do the delete from the JournalView, it will still crash as the NavigationLink is still referencing the object.

(即使我从JournalView中删除,由于NavigationLink仍在引用该对象,它仍然会崩溃。)

Interesting it will not crash if deleting a NavigationLink that has not yet been clicked on.

(有趣的是,如果删除尚未单击的NavigationLink,它不会崩溃。)

struct EntryView: View {

    @Environment(.managedObjectContext) private var context
    @Environment(.presentationMode) private var presentationMode

    @ObservedObject var entry: Entry

    var body: some View {
        Form {

            DatePicker(selection: $entry.date) {
                Text("Date")
            }

            Button(action: {
                self.context.delete(self.entry)
                self.presentationMode.wrappedValue.dismiss()
            }) {
                Text("Delete")
            }
        }
    }
}

UPDATE

(更新)

The crash is taking me to the first use of entry in the EntryView and reads Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).. thats the only message thrown.

(崩溃使我第一次进入EntryView中的条目,并读取线程1:EXC_BAD_INSTRUCTION(code = EXC_I386_INVOP,subcode = 0x0)..那就是抛出的唯一消息。)

The only work around I can think of is to add a property to the core data object "isDeleted" and set it to true instead of trying to delete from context.

(我能想到的唯一解决方法是向核心数据对象“ isDeleted”添加属性并将其设置为true,而不是尝试从上下文中删除。)

Then when the app is quit, or on launch, I can clean and delete all entries that isDeleted?

(然后,当应用程序退出或启动时,我可以清除并删除所有isDeleted吗?)

Not ideal, and would prefer to figure out what it wrong here, as it appears I'm not doing anything different then the MasterDetailApp sample, which seems to work.

(这不是理想的选择,而是希望在这里找出问题所在,因为看来我与MasterDetailApp示例并没有做任何不同的事情,该示例似乎可以正常工作。)

  ask by SybrSyn translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...