Search code examples
swiftarchitectureclean-architecture

Should I use Delegation or DataStore to passing data BACKWARD in Clean Swift architecture?


Now i'm trying to learn about Clean Swift architecture.

I'm using DataStore to passing data to next viewController and calling backward-viewController's interactor to do something when I want to send data backward like delegation.

I don't sure that this is suit with architecture. Please show me how.

I know that it is good to use DataStore for passing data FORWARD to next viewController. But is it good to use DataStore for passing data BACKWARD?

As all of my tableViewCells, collectionViewCells, etc. using delegation to sending any actions or datas to it's viewController, Will it be good and less confusing to use delegation for sending data BACKWARD too?

Thanks!


Solution

  • Using Delegation is the preferred way to do it in iOS, because it allows you to keep code components decoupled. The current view controller should not be really concerned who is its delegate, whether it's another view controller or some coordinator.

    To recap:

    • For passing data forward, use Dependency Injection
    • To pass data backwards, use Delegation

    For passing data backwards, we can also use Closure Properties, but personally I am not a big fan of doing it this way.