I was load testing my app that lists some todos and essentially pre-populated database with 1000 records. This slowed things down in the app dramatically, tab switching felt sluggish and I pinpointed it down to using
@Query var todos: [Todo]
However if I replace this query with a computed property that uses FetchDescriptor i.e.
var todos: [Todo] {
var descriptor = FetchDescriptor<Todo>()
do { return try DatabaseContainer.mainContext.fetch(descriptor) }
catch { return [] }
}
Everything is suddenly super fast (albeit observable nature of query is lost). Why is there such a drastic performance difference?
@Query
– takes ~2-3s to resolve
FetchDescriptor
– is instant
Is this a known limitation / bug in SwiftData?