Post

Replies

Boosts

Views

Activity

Reply to SwiftData equivalent of NSFetchedResultsController
Part 1: MVVM is not a great justification for the need of a NSFetchedResultsController MVVM is discouraged when using SwiftUI. To a certain extent, SwiftUI Views are the ViewModels. Assuming you use MVVM correctly, meaning that the lifecycle of your ViewModel is tied to the lifecycle of its related View, then MVVM should be replaced with MVVW, where VW stands for "ViewWrapper" (which is itself a View). Having a ViewWrapper should allow you to decouple the View from your model layer, hence allowing for View reusing and testing. To relate this comment to your question, you could use @Query in your "ViewWrapper" and pass your transformed data to your View. Part 2: Can we justify the need for NSFetchedResultsController differently? Yes. Allowing powerful usage of SwiftData with UIKit is the most obvious example. NSFetchedResultsController is all about tracking and diffing, which is built-in for SwiftUI. So building a NSFetchedResultsController for SwiftUI usage feels redundant. With this in mind, the fact that Apple didn't introduce NSFetchedResultsController is a rather telling proof that Apple built SwiftData for SwiftUI usage before all. It's not something they hide. Part 3: Forgetting about the MVVM pattern, what about fetches that happen independently of Views lifecycles. The tracking and diffing provided by NSFetchedResultsController is built to power views. It's not my opinion, it's Apple opinion. The documentation description says: "A controller that you use to manage the results of a Core Data fetch request and to display data to the user.". With this in mind, I think it's not needed/recommended to use NSFetchedResultsController in your data layer, repository layer, domain layer or wherever your business logic lives. Conclusion: My guess is that adoption of SwiftData will be slowed down drastically by its lack of compatibility with UIKit. And UIKit is not going anywhere. So, I assume Apple will eventually release a replacement for NSFetchedResultsController for the SwiftData world. But, if you use SwiftUI, the lack of NSFetchedResultsController should not be blocking you.
Jun ’23