r/SwiftUI•
tdcsf

Confused about the SelectionValue parameter in Lists vs CoreData

I have some working SwiftUI code that displays and can select the result of a fetch.

My code is essentially (edited to remove some not important flourishes)

@FetchRequest(sortDescriptors: []) private var players: FetchedResults<PlayerEntity>
@State private var selectedPlayerID : PlayerEntity.ID?

var body: some View {
    
    NavigationSplitView {
        List(players, selection: $selectedPlayerID) { player in
            Text(player.shortName ?? "")
        }
        .navigationTitle("Players")
    } detail: {
        if let id = selectedPlayerID, let player = players.first(where: {$0.id == id}) {
            Text("Do Something")
        }
    }
}

So here the selection is controlled by an optional managed object.id parameter. I downloaded the Apple Sample migrating to SwiftData ("SampleTrips") and in the CoreData version of the app, they are essentially doing:

    @FetchRequest(sortDescriptors: [SortDescriptor(\.startDate)])
    private var trips: FetchedResults<Trip>

    @State private var showAddTrip = false
    @State private var selection: Trip?
    @State private var path: [Trip] = []
    
    var body: some View {
        NavigationSplitView {
            List(selection: $selection) {
                ForEach(trips) { trip in
                    TripListItem(trip: trip)
//... removed some extra code
                }
            }
//... removed some extra code
            .navigationTitle("Upcoming Trips")
//... removed some extra code
        } detail: {
            if let selection = selection {
                NavigationStack {
                    TripDetailView(trip: selection)
                }
            }
        }

In this version, the selection is controlled by an optional managed object itself. I don't know why this works. Anyone have any ideas? Is that new in iOS 17?

Posted Aug 31, 2023, 10:48 PM UTC

15
[deleted]•

[deleted]

1

I believe selectionvalue just needs to conform to hashable

1
tdcsfOP•

yeah, I think the managed object requires hashable as well, so it should just work. My example uses CloudKit but that shouldn't make a difference (I think)

1
tdcsfOP•

This code displays the list but doesn't navigate to the detail but if I change the selection to $selectedPlayerID it works

struct PB2: View {
@FetchRequest(sortDescriptors: []) private var players: FetchedResults<PlayerEntity>
	
@State private var selectedPlayerID : PlayerEntity.ID?
@State private var selectedPlayer : PlayerEntity?

var body: some View {
	NavigationSplitView {
		List(selection: $selectedPlayer) {
			ForEach(players) { player in
				Text(player.shortName ?? "")
			}
		}
		.navigationTitle("Players")
	} detail: {
		Text("detail")
	}
}

}

1
tdcsfOP•

Some updates as I rathole here :) Building on iOS 17 doesn't change anything, nor does changing to a non CloudKit container. Mystery continues...

1
tdcsfOP•

After some more exploration, I finally noticed that the TripListItem included a NavigationLink, which seems to be what enables this:

Not entirely sure why but this code is working

var body: some View {
NavigationSplitView {
	List(players, selection: $selectedPlayer) { player in
		NavigationLink(value: player) {
	        	Text(player.shortName ?? "")
		}
	}
	.navigationTitle("Players")
} detail: {
	if let p = selectedPlayer {
		Text("detail \(p.shortName ?? "")")
	} else {
		Text("detail empty")
	}
}
1