Post not yet marked as solved
Any simple guidance or suggestions on this?
2024 here and the suggested solution doesn't work for me. I modified the Xcode template project for 'CoreData' such that the "item delete" occurs in a subview as a 'delete' button action. On delete I called one of:
extension NSManagedObjectContext {
public func singleDelete (_ object: NSManagedObject) {
self.delete(object)
try! self.save()
}
public func atomicDelete (_ object: NSManagedObject) {
self.perform {
self.delete(object)
try! self.save()
}
}
}
the atomicDelete helper extension being the suggested solution herein (less the do/catch logic). Both crashed in '#Preview' and on a simulator. Here is the subview:
extension Item {
var label: String {
"\(uuid!.uuidString.components(separatedBy: "-")[0]) (@ \(itemFormatter.string(from: timestamp!)))"
}
}
struct ItemBriefView: View { // Shown in navigationStack `List/ForEach`
@Binding var item: Item
var body: some View {
Text(item.label)
}
}
struct ItemView: View {
@Environment(\.managedObjectContext) private var context
@Environment(\.dismiss) var dismiss
var item: Item
var body: some View {
Form {
Text(item.label).padding(.bottom, 20)
Button ("Delete") {
context.atomicDelete(item)
dismiss()
}
}
}
}
The crashes are in the forced unwraps in \Item.lablel. (Telling me to use ?? is NOT a solution; I initialize every single managed object property and relationship).
Now, the solution/work-around I'm using is a wrapper view defined and used as such:
struct ItemView: View {
@Environment(\.managedObjectContext) private var context
@Environment(\.dismiss) var dismiss
var item: Item
var body: some View {
ManagedObjectDeleteWrapper (object: item) { item in
Form {
Text(item.label).padding(.bottom, 20)
Button ("Delete") {
context.singleDelete(item)
dismiss()
}
}
}
}
}
struct ManagedObjectDeleteWrapper<Object, Root> : View where Object : NSManagedObject, Root : View {
var object: Object
var root: (Object) -> Root
var body: some View {
Group {
if object.isFault { EmptyView() }
else { root (object) }
}
}
}
With this, one can detect a slight flash in the UI - which is better than a crash and the least of my SwiftUI 'oh, why is that happening' problems.
Cheers.