Monday, November 9, 2026
HomeiOS Developmentios - Deleting merchandise in information mannequin brought on SwiftUI app to...

ios – Deleting merchandise in information mannequin brought on SwiftUI app to crash

[ad_1]

I just lately began writing my first SwiftUI app and bumped into an design concern, which could be demonstrated by the easy code under. I do know what the problem is, however I’m wondering why it occurred and what is the advisable method to tackle it.

Within the demo, there are two views. The primary is a listing view, the second is a element view. The element view incorporates a button to delete the merchandise being displayed. Clicking on the button crashes the app as a result of pressured unwrapping within the element view. I did not count on the crash, as a result of from my understanding when the info mannequin adjustments, SwiftUI ought to regenerate all the view hierarchy. That’s, it calls ContentView.physique, which calls FooListView.physique, which works by way of objects within the information mannequin and creates NavigationLink for every merchandise.

Because the information mannequin has been modified, there is just one merchandise left. So I do not assume SwiftUI would create FooDetailView (or name its physique) for the merchandise deleted. If that’s the case, how come the FooDetailView code crashed? I attempted to debug the code however did not discover a lot helpful info. I consider the FooDetailView that crashed the app is the one which contained the deleted merchandise. That I do not perceive. Since SwiftUI regenerates view hierarchy, how might that outdated view left uncleaned up?

Can anybody clarify a bit the way you perceive it? And the way do you tackle the problem? I at the moment assume out two methods. The primary is to go all of the params wanted by element view to keep away from accessing information mannequin. However I do not assume this method scales. The second is to not use pressured wrapping. That ought to work fantastic, however I doubt if that is the advisable method to do it.

BTW, one other related setup to generate the crash is to make use of three views: record view -> element view -> delete view. When consumer clicks on button in delete view, the element view will crash.

Thanks.

import SwiftUI

struct Foo: Identifiable {
    var id: Int
    var worth: Int
}

class DataModel: ObservableObject {
    @Revealed var foos: [Foo] = [Foo(id: 1, value: 1), Foo(id: 2, value: 2)]
}

struct FooListView: View {
    @StateObject var dataModel = DataModel()
    
    var physique: some View {
        NavigationView {
            Listing {
                ForEach(dataModel.foos) { foo in
                    NavigationLink {
                        FooDetailView(fooID: foo.id)
                    } label: {
                        Textual content("(foo.worth)")
                    }
                }
            }
        }
        .environmentObject(dataModel)
    }
}

struct FooDetailView: View {
    @EnvironmentObject var dataModel: DataModel
    var fooID: Int
    
    var physique: some View {
        // Concern: the pressured unwrapping might crashe the app!
        let index = dataModel.foos.firstIndex(the place: { $0.id == fooID })! 
        
        VStack {
            Textual content("(dataModel.foos[index].worth)")
            Button("Delete It") {
                dataModel.foos.take away(at: index)
            }
        }
    }
}

struct ContentView: View {
    var physique: some View {
        FooListView()
    }
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments