Tuesday, January 9, 2024

SwiftData Fetching Pending Changes

Keith Harrison:

If you don’t want the pending changes included in the fetch results you should be able to override the default by setting includePendingChanges to false in the fetch descriptor[…] Unfortunately, I don’t seem to be able to get that to work using iOS 17.2 (FB13509125). The pending change is always returned in the results.

It’s interesting that the fetch results are different than with Core Data. It’s not clear whether there’s a bug with propagating in-memory changes to the managed object or whether SwiftData is using its own predicate evaluation for unsaved objects, leading to different performance characteristics and behavior.

The way pending changes interacts with a fetch limit also seems a little odd with SwiftData. […] It seems that SwiftData is including the pending change in the result without taking into account the fetch limit.

And I don’t understand why it’s generating SQL with LIMIT 2 instead of LIMIT 1. These bugs where there’s no error reported and it silently gives you incorrect results are just awful.


Comments RSS · Twitter · Mastodon

Leave a Comment