Dynamic FetchRequest in SwiftUI

Jerry PM
3 min readNov 28, 2024

As a passionate iOS developer, one of the most exciting features in SwiftUI for me has been the ease of data management. When SwiftUI’s FetchRequest met Core Data, it felt like magic. But as projects grew more complex, I realized there were challenges—one of the most common being dynamic filtering. Let me walk you through my journey of solving this problem, and hopefully, this article will save you hours of debugging and make your SwiftUI app even more powerful.

The Problem: A Static FetchRequest

Early in my SwiftUI journey, I built an app that displayed a list of tasks stored in Core Data. At first, everything worked perfectly. My FetchRequest fetched all the tasks, and I displayed them in a simple List. Then, a new requirement came in: filter the tasks dynamically based on their status—pending, completed, or all.

This was the challenge. FetchRequest in SwiftUI is static by design. Once defined, its predicate can’t be changed dynamically. Every time I tried to modify it, SwiftUI threw errors or refused to update the UI.

I started looking for solutions.

The First Attempt: Multiple FetchRequests

My initial thought was straightforward: create separate FetchRequest instances for each filter. Something like this:

--

--

No responses yet