I used to filter using initializers like this(example):
struct ArticleView: View {
@Environment(.modelContext) private var modelContext
@Query private var articleStates: [ArticleState]
let article: Article
init(article: Article) {
self.article = article
_articleStates = Query(filter: #Predicate { $0.articleID == article.id } )
}
var body: some View {
... // SwiftUI view that depends on query data.
}
}
But now when I use this method my iPhone get hot and the app freezes, even Simulator shows overload of CPU
(https://i.sstatic.net/McqcZapB.png)
That method worked fine in ios 17 but now It doesn’t work anymore. Do you have any solution?
2
Answers
Based on this Answer
Try this approach using an intermediate property to avoid referencing the
article model in the predicate.
You can try using Lazy loading, that is calling the Query only when needed like this. I think having it in the init could be the reason