Use of NSFetchedResultsController in Clean Architecture

Viewed 1214

I've searched for an answer to this without much luck. This question is pretty much the same but the answer isn't very clear (at least to me!): Which it is the place for NSFetchedResultsController in VIPER architecture?

The NSFetchedResultsController seems like a very useful approach for iOS apps but all the examples I've seen place this very much at the ViewController layer - at least, the VC becomes a delegate. In a Clean Architecture/Viper, the model layer is very much disconnected from the View layer and I can't figure out how the NSFRC is used in such an architecture. The answer to the above question implies that the Interactor should be a delegate but that doesn't make sense - the Managed Objects would then be surfaced to the Interactor, rather than PONSOs. Perhaps I don't understand it well enough yet, but (a) does it have a place in a Clean Architecture; and (b) if it does, then wants the right Swift implementation pattern?

3 Answers

You can see the NSFetchedResultController as a controller or adapter.

I don't agree with ad-johnson that the NSFRC belong in model.

The Dependency Rule in Clean Architecture says that dependencies must point inward. It doesn't say that use cases or adapters should be unaware of the model layer.

Maybe NSFetchedResultsController makes the tightest couplings between data to view layers in iOS projects. To solve that I use a wrapper around NSFetchedResultsController in a similar way to what @ad-johnson explained. I have also created an Xcode template that creates the required protocol for me automatically in projects.

I think I have succeeded to separate NSFetchedResultsController from another layers of the app completely through boundaries without loosing NSFetchedResultController routine features. So I can easily test, modify and even replace all layers easily du loose coupling between layers.

If you like to know more about it, here in GitHub is a project which is called CarOwnership that describes how to approach NSFetchedResultsController in a clean architecture. You can also find it here in my blog.

Related