modelContext.fetchIdentifiers(descriptor) Errors when Using a SortDescriptor

modelContext.fetchIdentifiers(descriptor) errors when using a SortDescriptor to sort by a variable and returns no models. The fetch works fine without a SortDescriptor, thus

FetchDescriptor<MyModel>()

works fine, but

FetchDescriptor<MyModel>(sortBy: [.init(\.title)])

or

FetchDescriptor<MyModel>(sortBy: [SortDescriptor(\.title)])

errors with console message

The operation couldn’t be completed. (SwiftData.SwiftDataError error 1.)

I am using Xcode Version 16.0 beta 6 (16A5230g).

Answered by Frameworks Engineer in 801878022

The error - sortingPendingChangesWithIdentifiers - indicates that sorting cannot be done if the FetchDescriptor includes pending changes in the ModelContext.

Set the FetchDescriptor to NOT include pending changes:

fetchDesc.includePendingChanges = false

Accepted Answer

The error - sortingPendingChangesWithIdentifiers - indicates that sorting cannot be done if the FetchDescriptor includes pending changes in the ModelContext.

Set the FetchDescriptor to NOT include pending changes:

fetchDesc.includePendingChanges = false

modelContext.fetchIdentifiers(descriptor) Errors when Using a SortDescriptor
 
 
Q