简体   繁体   中英

Why does SwiftUI automatically navigate backwards when my underlying data source is updated and how can I avoid this behavior?

I have a fairly common set up:

  • A list of items created using the ForEach component
  • When an item is tapped my app navigates to a detailed view
  • Inside of the detailed view, the user can perform updates that change the underlying item

The problem is that updating the underlying item (which is a struct) causes SwiftUI to automatically navigate backward. I assume this is because the struct is an immutable value and gets destroyed during the update, however, it does conform to Identifiable so I'm expecting SwiftUI to understand that the item still exists and just needs to be updated rather than destroyed.

Is there any way to update the underlying list without navigating away from the detail view?

Here's a minimal, reproducible example.

import SwiftUI

struct ContentView: View {
    var body: some View {
       DemoList(viewModel: ViewModel())
    }
}

struct DemoItem: Codable, Hashable, Identifiable {
    var id: UInt
    var description: String
}

final class ViewModel: ObservableObject, Identifiable {
    @Published var list = [
        DemoItem(id: 1, description: "One"),
        DemoItem(id: 2, description: "two")
    ]
    
    /// This update causes SwiftUI to automatically navigate away from the detail view
    func update(item: DemoItem) {
        list = list.map { $0.id == item.id ? item : $0 }
    }
}

struct DemoList: View {
    @ObservedObject var viewModel: ViewModel
    
    var body: some View {
        NavigationView {
            ForEach(viewModel.list, id: \.self) { item in
                NavigationLink(destination: DemoDetail(viewModel: self.viewModel, item: item)) {
                    Text(item.description)
                }
            }
        }
    }
}

struct DemoDetail: View {
    @ObservedObject var viewModel: ViewModel
    var item: DemoItem
    
    var body: some View {
        Text(item.description)
            .onTapGesture {
                let newItem = DemoItem(id: self.item.id, description: UUID().uuidString)
                self.viewModel.update(item: newItem)                
            }
    }
}

Just remove the id from the ForEach and it'll stop navigating back

ForEach(viewModel.list) { item in
      NavigationLink(destination: DemoDetail(viewModel: self.viewModel, item: item)) {
      Text(item.description)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM