Does ModelContext fetch() return an auto-updating results array?

I was wondering if ModelContext's fetch() contains an auto-updating results array? I ask because there is no documentation yet and in the WWDC23 lounge it was suggested by Debbie G to use fetch() to overcome the limitations of @Query i.e. no way to use dynamic filtering or sorting. I suppose we would call fetch() from onAppear and onChanged to support dynamic queries that also have auto-updating of results. Personally I don't understand why @Query and the old @FetchRequest were implemented as property wrappers instead of SwiftUI modifiers, e.g. .fetch(predicate:sort:) to match other modifiers like .task(id:).

I was hoping for it to behave similar to Date's formatted() that returns a locale-aware string that automatically invalidates SwiftUI Text when the locale changes. Although I'm not exactly sure how it works.

If fetch() doesn't auto-update then what would be the point of using it instead of just using NSFetchRequest with dictionary result type to get data into a SwiftUI view struct fast and memory efficient.

Post not yet marked as solved Up vote post of malc Down vote post of malc
782 views
  • edit - "I suppose we would call fetch() from onAppear and onChanged" I meant fetch() is called from computed property that is used in a ForEach.

Add a Comment