Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

SwiftUI sample app using Clean Architecture. Examples of working with SwiftData persistence, networking, dependency injection, unit testing, and more.

License

NotificationsYou must be signed in to change notification settings

nalexn/clean-architecture-swiftui

Repository files navigation


Clean Architecture for SwiftUI + Combine

A demo project showcasing the setup of the SwiftUI app with Clean Architecture.

The app uses therestcountries.com REST API to show the list of countries and details about them.

Check outmvvm branch for the MVVM revision of the same app.

For the example of handling theauthentication state in the app, you can refer to myother tiny project that harnesses the locks and keys principle for solving this problem.

platformscodecovcodebeat badge

Diagram

Key features

  • End of 2024 update: the project was fully revamped to use modern iOS stack technologies
  • DecoupledPresentation,Business Logic, andData Access layers
  • Programmatic navigation. Push notifications with deep link
  • Redux-like centralizedAppState as the single source of truth
  • Native SwiftUI dependency injection
  • Handling of the system events (such asdidBecomeActive,willResignActive)
  • Full test coverage, including the UI (thanks to theViewInspector)
  • Simple yet flexible networking layer built on async - await
  • UI - vanillaSwiftUI +Combine
  • Data persistence withSwiftData

Architecture overview

Diagram

Presentation Layer

SwiftUI views that contain no business logic and are a function of the state.

Side effects are triggered by the user's actions (such as a tap on a button) or view lifecycle eventonAppear and are forwarded to theInteractors.

State and business logic layer (AppState +Interactors) are natively injected into the view hierarchy with@Environment.

Business Logic Layer

Business Logic Layer is represented byInteractors.

Interactors receive requests to perform work, such as obtaining data from an external source or making computations, but they never return data back directly.

Instead, they forward the result to theAppState or to aBinding. The latter is used when the result of work (the data) is used locally by one View and does not belong to theAppState.

Previously, this app did not use CoreData for persistence, and all loaded data were stored in theAppState.

With the persistence layer in place we have a choice - either to load the DB content onto theAppState, or serve the data fromInteractors on an on-demand basis throughBinding.

The first option suits best when you don't have a lot of data, for example, when you just store the last used login email in theUserDefaults. Then, the corresponding string value can just be loaded onto theAppState at launch and updated by theInteractor when the user changes the input.

The second option is better when you have massive amounts of data and introduce a fully-fledged database for storing it locally.

Data Access Layer

Data Access Layer is represented byRepositories.

Repositories provide asynchronous API (Publisher from Combine) for makingCRUD operations on the backend or a local database. They don't contain business logic, neither do they mutate theAppState. Repositories are accessible and used only by the Interactors.


Twitterblog

About

SwiftUI sample app using Clean Architecture. Examples of working with SwiftData persistence, networking, dependency injection, unit testing, and more.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp