EditButton causes the selection in sidebar view to be lost

Problem

When app is run on iPhone 14 pro simulator running iOS 17 tapping on the EditButton shows the text "No folder selected"

Feedback

Feedback ID: FB12953838

Steps to reproduce:

  1. Run the app on iOS 17 iPhone simulator
  2. Tap on the "Edit" Button

Expected Behaviour

The view should show the car list in the edit mode

Actual Behaviour

The view shows the text "No folder selected"

Note: Problem happens only the first time, subsequently EditButton works fine.

Environment:

  • iOS: 17
  • Platform: iPhone 14 pro simulator
  • Xcode: 15.0 beta 6 (15A5219j)

Code

ContentView

import SwiftUI

struct ContentView: View {
    
    @State private var selectedFolderID: Int?
    @StateObject private var dataStore = DataStore()
    
    var body: some View {
        NavigationSplitView {
            FolderListView(
                selectedFolderID: $selectedFolderID,
                dataStore: dataStore
            )
        } detail: {
            if let selectedFolderID {
                CarListView(
                    selectedFolderID: selectedFolderID,
                    dataStore: dataStore
                )
            } else {
                Text("No folder selected")
            }
        }
    }
}

FolderListView

import SwiftUI

struct FolderListView: View {
    @Binding var selectedFolderID: Int?
    @ObservedObject var dataStore: DataStore

    var body: some View {
        List(dataStore.folders, selection: $selectedFolderID) { folder in
            NavigationLink(value: folder.id) {
                Text(folder.name)
            }
        }
        .task {
            selectedFolderID = dataStore.folders.first?.id
        }
    }
}

CarListView

import SwiftUI

struct CarListView: View {
    let selectedFolderID: Int
    @ObservedObject var dataStore: DataStore
    @State private var selectedCarIDs = Set<Int>()
    var body: some View {
        List(
            dataStore.cars(withFolderID: selectedFolderID),
            selection: $selectedCarIDs
        ) { car in
            Text(car.name)
        }
        .toolbar {
            //Tapping on the EditButton on the iPhone, shows the text "No folder selected".
            EditButton()
        }
    }
}

DataStore

import Foundation

class DataStore: ObservableObject {
    @Published var folders = [Folder(id: 0, name: "Folder 1"),
                              Folder(id: 1, name: "Folder 2"),
                              Folder(id: 2, name: "Folder 3"),
                              Folder(id: 3, name: "Folder 4"),
                              Folder(id: 4, name: "Folder 5")]
    
    @Published var carIDsInFolder: [Folder.ID : [Car.ID]] = [0: [0, 1],
                                                             1: [2, 3],
                                                             2: [4, 5],
                                                             3: [6, 7],
                                                             4: [8, 9]]
    
    @Published var cars = [Car(id: 0, name: "aaa", price: 100),
                           Car(id: 1, name: "bbb", price: 110),
                           Car(id: 2, name: "ccc", price: 120),
                           Car(id: 3, name: "ddd", price: 130),
                           Car(id: 4, name: "eee", price: 140),
                           Car(id: 5, name: "fff", price: 150),
                           Car(id: 6, name: "iii", price: 160),
                           Car(id: 7, name: "jjj", price: 170),
                           Car(id: 8, name: "***", price: 180),
                           Car(id: 9, name: "lll", price: 190)]
    
    func cars(withFolderID folderID: Folder.ID) -> [Car] {
        let carIDs = carIDsInFolder[folderID] ?? []
        return carIDs.compactMap { car(withID: $0) }
    }
    
    func car(withID carID: Car.ID) -> Car? {
        cars.first { $0.id == carID }
    }
}

Car

import Foundation

struct Car: Identifiable {
    var id: Int
    var name: String
    var price: Int
}

Folder

import Foundation

struct Folder: Identifiable {
    var id: Int
    var name: String
}

Any help on this would be great!

Any help or workaround would be much appreciated, I have filed a feedback FB12953838

EditButton causes the selection in sidebar view to be lost
 
 
Q