Clean Architecture

  • You will have massive view controllers in MVC
  • So you’ve tried MVVM with ReactiveCocoa, but you have to learn a new thing. More dependencies
  • Or even VIPER. But setting up all the interfaces in the wireframe is so complex. And you can’t use segues.

https://clean-swift.com/clean-swift-ios-architecture/

Advantages

  • Find and fix bugs faster and easier.
  • Change existing behaviors with confidence.
  • Add new features easily.
  • Write shorter methods with single responsibility.
  • Decouple class dependencies with established boundaries.
  • Extract business logic from view controllers into interactors.
  • Build reusable components with workers and service objects.
  • Write factored code from the start.
  • Write fast and maintainable unit tests.
  • Have confidence in your tests to catch regression. Apply what you learn to new and existing projects of any size.

Organizing Your Code in Xcode

  • Group and file names should reveal your intentions for the use cases.
  • It should not reflect the underlying framework structure.
  • In Clean Swift, your project structure is built around scenes (or screens)
  • This organization tells you far more than the model, view, and controller groups you are used to see.

The VIP Cycle

The view controller, interactor, and presenter are the three main components of Clean Swift. They act as input and output to one another as shown in the following diagram.

View Controller

  • We extract display logic in a protocol
  • The argument to doSomething() is a request object that is passed through the boundary from the view controller to the interactor.
  • The view controller doesn’t and shouldn’t care who and how it is done.
  • This means we have decoupled the underlying order data model from the view controller and interactor.

Interactor

  • The interactor contains your app’s business logic
  • The user taps and swipes in your UI in order to interact with your app.
  • The view controller collects the user inputs from the UI and passes it to the interactor.
  • It then retrieves some models and asks some workers to do the work.

Worker A profile view may need to fetch the user from Core Data, download the profile photo, allows users to like and follow, …etc. You don’t want to swamp the interactor with doing all these tasks. Instead, you can break it down into many workers, each doing one thing. You can then reuse the same workers elsewhere.

Presenter After the interactor produces some results, it passes the response to the presenter. The presenter then marshal the response into view models suitable for display. It then passes the view models back to the view controller for display to the user.

Router

  • A router extracts this navigation logic out of the view controller.
  • It is also the best place to pass any data to the next scene.
  • As a result, the view controller is left with just the task of controlling views.

results matching ""

    No results matching ""