Swiftui App Crashes Every Time When Trying to Add Item to Grouped Items

SwiftUI: App crashes when resizing ScrollView with 3 TextField inside HStack on macOS

Here is workaround. Fixed variant tested with Xcode 11.4 / macOS 10.15.4

struct CrashedContentView: View {
var body: some View {
GeometryReader { gp in
ScrollView {
HStack {
ForEach(1...3, id: \.self) { idx in
TextField("", text: .constant("text \(idx)"))
}
}.frame(width: gp.size.width)
}
}
}
}

SwiftUI List crashes when removing a selected item

Re-tested after installing MacOS 11.0 Beta 7 (20A5374g).
The example code doesn’t crash any more, the bug seems to be fixed.

Thanks to all for testing and so giving me the hint, that it's a MacOS beta bug. :-)

SwiftUI holding reference to deleted core data object causing crash

I basically had the same issue. It seems that SwiftUI loads every view immediately, so the view has been loaded with the Properties of the existing CoreData Object. If you delete it within the View where some data is accessed via @ObservedObject, it will crash.

My Workaround:

  1. The Delete Action - postponed, but ended via Notification Center
    Button(action: {
//Send Message that the Item should be deleted
NotificationCenter.default.post(name: .didSelectDeleteDItem, object: nil)

//Navigate to a view where the CoreDate Object isn't made available via a property wrapper
self.presentationMode.wrappedValue.dismiss()
})
{Text("Delete Item")}

You need to define a Notification.name, like:

extension Notification.Name {

static var didSelectDeleteItem: Notification.Name {
return Notification.Name("Delete Item")
}
}

  1. On the appropriate View, lookout for the Delete Message

// Receive Message that the Disease should be deleted
.onReceive(NotificationCenter.default.publisher(for: .didSelectDeleteDisease)) {_ in

//1: Dismiss the View (IF It also contains Data from the Item!!)
self.presentationMode.wrappedValue.dismiss()

//2: Start deleting Disease - AFTER view has been dismissed
DispatchQueue.main.asyncAfter(deadline: .now() + TimeInterval(1)) {self.dataStorage.deleteDisease(id: self.diseaseDetail.id)}
}


  1. Be safe on your Views where some CoreData elements are accessed - Check for isFault!

VStack{
//Important: Only display text if the disease item is available!!!!
if !diseaseDetail.isFault {
Text (self.diseaseDetail.text)
} else { EmptyView() }
}

A little bit hacky, but this works for me.

TextField with animation crashes app and looses focus

  1. How can I keep the focus of the TextField after the body is
    recalculated because of a change in state?

You have two of them. Two different TextField could not be in editing state at the same time.

The approach suggested by Asperi is the only possible.

The reason, why your code crash is not easy explain, but expected in current SwiftUI.

You have to understand, that Group is not a standard container, it just like a "block" on which you can apply some modifiers. Removing Group and using wraping body in ViewBuilder

struct CustomTextFieldView: View {

@Binding var text: String
@State var editing: Bool = false

@ViewBuilder
var body: some View {
if self.editing {
TextField("", text: $text)
.background(Color.red)
.onTapGesture {
self.editing.toggle()
}
} else {
ZStack(alignment: .leading) {
TextField("", text: $text)
.background(Color.green)
.onTapGesture {
self.editing.toggle()
}
}
}

}
}

the code will stop to crash, but there is other issue, the keyboard will dismiss immediately. That is due the tap gesture applied.

So, believe or not, you have to use ONE TextField ONLY.

struct CustomTextFieldView: View {

@Binding var text: String
@State var editing = false

var textField: some View {
TextField("", text: $text, onEditingChanged: { edit in
self.editing = edit
})
}

var body: some View {

textField.background(editing ? Color.green : Color.red)

}
}

Use this custom text field elsewhere in your code, as you want



Related Topics



Leave a reply



Submit