Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a List of TextFields in SwiftUI without breaking deletion

When I

  1. Create a Master-Detail App in XCode
  2. Use Core Data
  3. Add a new field to the Event Model (say title as String)
  4. Change the MasterView implementation to this
struct MasterView: View {
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Event.timestamp, ascending: true)], 
        animation: .default)
    var events: FetchedResults<Event>

    @Environment(\.managedObjectContext)
    var viewContext

    var body: some View {
        List {
            ForEach(events, id: \.self) { event in
                NavigationLink(
                    destination: DetailView(event: event)
                ) {
                    TextField("Title", text: Binding(ObservedObject<Event>(wrappedValue: event).projectedValue.title)!)
                }
            }.onDelete { indices in
                self.events.delete(at: indices, from: self.viewContext)
            }
        }
    }
}

, i.e., swap out the Text for TextField with an appropriate binding to the title attribute of the event object, XCode happily compiles and the app runs with the expected behaviour as long as I don't try to delete from the List.

The moment I try to delete an Event, it crashes with this stacktrace

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
    #0: [33m`SwiftUI.BindingOperations.ForceUnwrapping.get(base: Swift.Optional<A>) -> A[0m
    #1: [33m`protocol witness for SwiftUI.Projection.get(base: A.Base) -> A.Projected in conformance SwiftUI.BindingOperations.ForceUnwrapping<A> : SwiftUI.Projection in SwiftUI[0m
    #2: [33m`SwiftUI.(ProjectedLocation in _5A9440699EF65619D724050F1A6941EE).update(context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>, state: inout Swift.Optional<Any>) -> (B.Projected, Swift.Bool)[0m
    #3: [33m`SwiftUI.LocationBox.update(context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>, state: inout Swift.Optional<Any>) -> (A.Value, Swift.Bool)[0m
    #4: [33m`SwiftUI.Binding.(ScopedLocation in _5436F2B399369BE3B016147A5F8FE9F2).update(context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>, state: inout Swift.Optional<Any>) -> (A, Swift.Bool)[0m
    #5: [33m`protocol witness for SwiftUI.Location.update(context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>, state: inout Swift.Optional<Any>) -> (A.Value, Swift.Bool) in conformance SwiftUI.Binding<A>.(ScopedLocation in _5436F2B399369BE3B016147A5F8FE9F2) : SwiftUI.Location in SwiftUI[0m
    #6: [33m`SwiftUI.LocationBox.update(context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>, state: inout Swift.Optional<Any>) -> (A.Value, Swift.Bool)[0m
    #7: [33m`SwiftUI.Binding.(Box in _5436F2B399369BE3B016147A5F8FE9F2).update(property: inout SwiftUI.Binding<A>, context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>) -> Swift.Bool[0m
    #8: [33m`static SwiftUI.(BoxVTable in _68550FF604D39F05971FE35A26EE75B0).update(ptr: Swift.UnsafeMutableRawPointer, property: Swift.UnsafeMutableRawPointer, context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>) -> Swift.Bool[0m
    #9: [33m`SwiftUI._DynamicPropertyBuffer.update(container: Swift.UnsafeMutableRawPointer, context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>) -> Swift.Bool[0m
    #10: [33m`SwiftUI.(DynamicPropertyBody in _9F92ACD17B554E8AB7D29ABB1E796415).update(context: inout AttributeGraph.AttributeContext<SwiftUI.(DynamicPropertyBody in _9F92ACD17B554E8AB7D29ABB1E796415)<A>>) -> ()[0m
    #11: [33m`protocol witness for static AttributeGraph.UntypedAttribute._update(_: Swift.UnsafeMutableRawPointer, graph: __C.AGGraphRef, attribute: __C.AGAttribute) -> () in conformance SwiftUI.(DynamicPropertyBody in _9F92ACD17B554E8AB7D29ABB1E796415)<A> : AttributeGraph.UntypedAttribute in SwiftUI[0m
    #12: [33m`partial apply forwarder[0m
    #13: [33m`AG::Graph::UpdateStack::update[0m
    #14: [33m`AG::Graph::update_attribute[0m
    #15: [33m`AG::Subgraph::update[0m
    #16: [33m`SwiftUI.ViewGraph.(runTransaction in _D63C4EB7F2B205694B6515509E76E98B)(in: __C.AGGraphRef) -> ()[0m
    #17: [33m`closure #1 (__C.AGGraphRef) -> (prefs: Swift.Bool, idealSize: Swift.Bool, outputs: SwiftUI.ViewGraph.Outputs) in SwiftUI.ViewGraph.updateOutputs(at: SwiftUI.Time) -> ()[0m
    #18: [33m`SwiftUI.ViewGraph.updateOutputs(at: SwiftUI.Time) -> ()[0m
    #19: [33m`closure #1 () -> () in closure #1 () -> () in (extension in SwiftUI):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> ()[0m
    #20: [33m`closure #1 () -> () in (extension in SwiftUI):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> ()[0m
    #21: [33m`(extension in SwiftUI):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> ()[0m
    #22: [33m`closure #1 () -> () in SwiftUI._UIHostingView.requestImmediateUpdate() -> ()[0m
    #23: [33m`reabstraction thunk helper from @escaping @callee_guaranteed () -> () to @escaping @callee_unowned @convention(block) () -> ()[0m
    #24: [33m`_dispatch_call_block_and_release[0m
    #25: [33m`_dispatch_client_callout[0m
    #26: [33m`_dispatch_main_queue_callback_4CF[0m
    #27: [33m`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__[0m
    #28: [33m`__CFRunLoopRun[0m
    #29: [33m`CFRunLoopRunSpecific[0m
    #30: [33m`GSEventRunModal[0m
    #31: [33m`UIApplicationMain[0m
  * #32: [33m`main[0m at AppDelegate.swift:13
    #33: [33m`start[0m

I have tried this: SwiftUI holding reference to deleted core data object causing crash but to no avail. I have also tried extracting the textfield into a separate view class and using the appropriate propertywrappers.

I suppose I am misusing the infrastructure (binding, observableobject, etc.) somehow. How am I supposed to use these to achieve textfields in a list?

like image 809
Holger Avatar asked Dec 10 '19 15:12

Holger


2 Answers

Some ideas on this:

Option 1 (Inline Binding)

(I'm guessing that there might be a cleaner implementation, but this code works fine)

// Somewhere in MasterView...
// A function that returns a binding for the title of an event
func titleBindingFor(_ event: Event) -> Binding<String> {
    Binding<String>(get: { () -> String in
        event.title ?? ""
    }) { (title) in
        event.title = title
    }
}

then

// Bind it to the textfield
TextField("Title", text: self.titleBindingFor(event))

Option 2 (Separate View)

extension Optional where Wrapped == String {
    var safe: String {
        get { self ?? "" }
        set { self = newValue }
    }
}

struct TitleEditor: View {
    @ObservedObject var event: Event

    var body: some View {
        TextField("Title", text: $event.title.safe)
    }
}

then

TitleEditor(event: event)

Note: I also had to handle the optional timestamp force unwrap (really Apple?!) in DetailView cause I was getting crashes on deletion:

struct DetailView: View {
    @ObservedObject var event: Event

    var body: some View {
        let timestamp = event.timestamp ?? Date()
        return Text("\(timestamp, formatter: dateFormatter)")
            .navigationBarTitle(Text("Detail"))
    }
}

Notice that I'm returning an empty string if the title is nil, which is handled gracefully by the textfield (it shows the placeholder on newly created items)

like image 145
Alladinian Avatar answered Sep 20 '22 05:09

Alladinian


Instead of

TextField("Title", text: Binding(ObservedObject(wrappedValue: event).projectedValue.title)!)

Use

TextField("Title", text: Binding<String>(
                   get: {event.title ?? "<none>"}, set: {event.title = $0}))

Tested & Works with Xcode 11.2, iOS 13.2

like image 36
Asperi Avatar answered Sep 23 '22 05:09

Asperi