iOS native app demo with Xcode and Swift using MVVM architecture and Apple's Combine framework for functional reactive programming, all with UIKit

Overview

iOS (Swift 5): MVVM test with Combine and UIKit

MVVM and functional reactive programming (FRP) are very used in iOS development inside companies. Here, there is an example where I implement the MVVM architecture with Combine, Apple's official framework, being the equivalent of the famous RxSwift framework. All with UIKit.

Navigation plan

MVVM architecture

MVVM architecture (Model View ViewModel) is a design pattern which allows to seperate business logic and UI interactions. Starting from MVC, the view and the controller are now one in MVVM. In iOS with UIKit, the ViewController belongs to the view part. Furthermore, the ViewController no longer have to manage business logic and no longer have references to data models.

The novelty being the View Model is that it has the responsibility to manage the business logic and update the view by disposing of properties that the view will display through data binding.

Data binding is a link between the view and the view model, where the view through user interactions will send a signal to the view model to run a specific business logic. This signal will allow Le data binding est un lien entre la vue et la vue modèle, où la vue par le biais des interactions avec l'utilisateur va envoyer un signal à la vue modèle afin d'effectuer une logique métier spécifique. This signal will allow the update of the data of the model data and thus allow automatic refresh of the view. Data binding in iOS can be done with:

  • Delegation
  • Callbacks (closures)
  • Functional Reactive Programming (RxSwift, Combine)

Pros and cons of MVVM

  • Main pros:
    • Suitable architecture to separate the view from business logic through a ViewModel.
    • ViewController lightened.
    • Business logic tests easier to do (Code coverage increased)
    • Suitable with SwiftUI
    • Suitable with reactive programming (RxSwift, Combine)
  • Cons:
    • ViewModel can be massive if the separation of the elements is not mastered, so it's hard to cut correctly structures, classes and methods in order to respect the 1st principle of SOLID being the SRP: Single Responsibility Principle. MVVM-C alternative with uses a Coordinator is useful to lighten the views and manage the navigation between views.
    • May be complex for very small projects.
    • Unsuitable for very large and complex projects, it will be better to switch to VIPER or Clean Architecture (VIP, MVVM, Clean Swift, ...). MVVM can be integrated inside a Clean Architecture.
    • Complex mastery for beginners (especially with UIKit)

MVVM

Functional Reactive Programming with Combine

The reactive programming is an asynchronous programming paradigm, oriented around data stream and the propagation of change. This model is based on the observer pattern where a stream creates data at different times. Actions are then executed in an orderly fashion.

These streams are modelized with Observables (Publishers with Combine) which will emit events of 3 types:

  • Value
  • Error
  • Completion (the stream has no data to send anymore)

Like event-based programming, the reactive programming uses also Observers (Subscribers with Combine) which will subscribe to the events, emitted by Observables, then receive the data (listening for changes) of the stream in real time in order to execute actions depending to the signal.

The 3rd element of reactive programming is named Subjects which acts as dual way, both as an Observable as well as Observer. Subjects can emit and receive events.

We talk about FRP: Functional Reactive Programming) the way of combining data streams with function type operators to process the data (formatting, value updating, filtering, merging several streams into one, ...), like those in the arrays with:

  • map
  • filter
  • flatMap
  • compactMap
  • reduce
  • And more...

Functional Reactive Programming is perfectly suitable for the data binding of MVVM architecture with an observable in the view model to emit the received events, especially asynchronous ones (network calls, GPS updating, model data updating, ...) and an observer in the view which will subscribe to the view model observable and listen to any change.

On the other hand, it is also necessary to use Cancellables which will cancel the subscription of the observers (AnyCancellable with Combine) and manage the memory deallocation in order to avoid memory leaks.

Functional reactive programming remains one of the most complex concepts to learn and master La programmation réactive fonctionnelle reste l'une des notions les plus complexes à apprendre et à maîtriser (especially by oneself in autodidact), the definition itself is complex to to understand and assimilate. But once mastered, this concept becomes a powerful weapon to write optimal asynchronous functionnalities (chaining HTTP calls, asynchronous server check before validation, ...), having reactive interface which updates itself automatically when changes appended in real time from the data stream, to replace delegation (passing data backward from secondary to main view, ...), ... Furthermore, knowing how to use reactive programming is also essential to integrate an iOS application project in a compan, being one of the most required skills.

Combine requires iOS 13 or above for any iOS application. The main advantage of Combine is at the level of performance and optimization, since everything is managed by Apple, and Apple can go at the deepest of the operating system elements, thing that third-party framework developers cannot do. External framework dependency is now reduced.

Compared to RxSwift, Combine remains less complete in terms of operators for specific and advanced cases. Also Combine is not fully suitable with UIKit especially for bindings with UI components, thing that is more complete with RxSwift (RxCocoa).

You might also like...
This a simple swiftui app where i used mvvm architecture, coredata swiftui and so on..
This a simple swiftui app where i used mvvm architecture, coredata swiftui and so on..

SwiftUI MVVM COREDATA NOTE APP This a simple swiftui app where i used mvvm architecture, coredata swiftui and so on... #FEATURES SWIFTUI MVVM COREDATA

Create a simple MVVM-C iOS architecture with Swift for starters

iOS-Architecture-MVVM MVVM+Coordinators IOS Architecture Tutorial By Bobby Pehtr

⚛️ A Reactive Data-Binding and Dependency Injection Library for SwiftUI x Concurrency.
⚛️ A Reactive Data-Binding and Dependency Injection Library for SwiftUI x Concurrency.

SwiftUI Atom Properties A Reactive Data-Binding and Dependency Injection Library for SwiftUI x Concurrency 📔 API Reference Introduction Examples Gett

A travel guide app that implements MVVM design pattern, using mock API.
A travel guide app that implements MVVM design pattern, using mock API.

✈️ Travel Guide App 🏖 A travel guide app that implements MVVM design pattern, using mock API. ✨ Features 🔸 Project Features Written in Swift Impleme

Explanations and samples about the Swift programming language
Explanations and samples about the Swift programming language

About Swift Contents Explanations and samples about: Swift Programming Language Swift Standard Library Target audience Developers familiar with object

ProductListSwiftUI - SwiftUI Project to fetch product list using the fakestoreapi and the MVVM architectural pattern
ProductListSwiftUI - SwiftUI Project to fetch product list using the fakestoreapi and the MVVM architectural pattern

ProductListSwiftUI SwiftUI Project to fetch product list using the fakestoreapi

SampleProjectMVVM - Sample project using MVVM parsing data from Giphy.com
SampleProjectMVVM - Sample project using MVVM parsing data from Giphy.com

iOS Take Home Create an iOS app with two views, MainViewController and DetailVie

NewsAppMVVM - A Swift iOS App created to practice MVVM Design Pattern
NewsAppMVVM - A Swift iOS App created to practice MVVM Design Pattern

NewsAppMVVM A Swift iOS App created to practice MVVM Design Pattern. This app re

MVVM-of-SuYeon - Build an Instagram iOS App Clone with Cloud Firestore, Swift 5

MVVM-of-SuYeon Instagram Firestore App Clone | Swift 5 + iOS 14 | MVVM Build an

Owner
Koussaïla BEN MAMAR
Ingénieur jeune diplômé d'EFREI Paris, majeure Software Engineering. Passionné par l'univers Apple, et en particulier iOS.
Koussaïla BEN MAMAR
MVVM-RXSWIFT-COMBINE- - Demo application populating posts from network request using

MVVM => RXSWIFT + COMBINE Demo application populating posts from network request

Amr Al-khayat 0 Jan 2, 2022
A demo demonstrates how to use combine and MVVM in the SwiftUI app

SwiftUI-MVVM-Combine A demo demonstrates how to use combine and MVVM in the Swif

Asa. Ga 7 Jul 5, 2022
Swift, UIkit, Anchorage, Clean Architecture, UICollectionViewDiffableDataSourcem, MVVM+Coordinators patterns

About the app iOS project realized with Swift, UIkit, Anchorage, Clean Architecture, UICollectionViewDiffableDataSource and MVVM + Coordinators patter

Luca Berardinelli 4 Dec 29, 2022
Demonstration blackjack app for native iOS. Uses MVVM architecture

Blackjack - native iOS application This project is a simple demonstration on how to intergrate swiftUI with MVVM architecture. Although, technically,

Michael Nguyen 2 Nov 28, 2022
SpaceX rocket listing app using RxSwift and CLEAN Architecture with MVVM

Jibble SpaceX rocket listing app using RxSwift and CLEAN Architecture with MVVM Demo Features Reactive Bindings URL / JSON Parameter Encoding Filter Y

Ammad Akhtar 0 Dec 5, 2021
Braze is a Crypto Currency App created using SwiftUI with MVVM architecture.

Braze A Crypto Currency App created using SwiftUI with MVVM architecture. Braze tracks live prices of crypto coins and can create mock portfolio. Usin

Roy 5 Dec 7, 2022
A sample project exploring MVVM pattern with SwiftUI/Combine, using Unsplash API (via Picsum.photos API)

CombineUnsplash A sample project exploring MVVM pattern with SwiftUI/Combine, using Unsplash API (via Picsum.photos API) with detail example. Resource

Vinh Nguyen 26 Dec 13, 2022
MockChat TDD MVVM SwiftUI - Mockable, Test driven Development, clear Architecture example using SwiftUI

MockChat_TDD_MVVM_SwiftUI Mockable, Test driven Development, clear Architecture

Zahirul Islam 0 Feb 5, 2022
An app to demo networking with SwiftUI/MVVM

Network Demo An app to demonstrate how network calls can be implemented in a SwiftUI/MVVM app. Motivations Apple's introductory tutorial is a great st

Tatsuya Kaneko 6 Oct 7, 2022
SignalKit is a reactive Swift framework with focus on clean and readable API.

Abstract SignalKit is a lightweight event and binding framework. The core of SignalKit is the Observable protocol. Each implementation of the Observab

Yanko Dimitrov 252 Dec 13, 2022