At 24:56 in "Bring Core Data concurrency to Swift and SwiftUI" there's the following discussion:
But here's the important part. Changes to the request are committed whenever the results getter is called, so to update both the sorting and the sectioning safely... I need to update the configuration on a reference to the results that I've pulled into a local.
The code in question is a property:
@SectionedFetchRequest(
sectionIdentifier: \.day,
sortDescriptors: [SortDescriptor(\Quake.time, order: .reverse)])
private var quakes: SectionedFetchResults<String, Quake>
And it is updated with:
.onChange(of: selectedSort) { _ in
let sortBy = sorts[selectedSort.index]
let config = quakes
config.sectionIdentifier = sortBy.section
config.sortDescriptors = sortBy.descriptors
}
It's unclear what the value/reference semantics here are. quakes
looks like a value type, but this is clearly treating it as a reference type. But if it's a reference type, why is the config
local variable important? It feels like some kind of magic is happening here.
Why is this local variable necessary, and how would I know this?