Theoretical usage of Repository Pattern with RxSwift and MVVM

Oguz Parlak
The Startup
Published in
3 min readFeb 8, 2020

Hey there,

In this article, I will be writing about usage of Repository Design Pattern and appliance into an iOS application including RxSwift and MVVM.

What is Repository ?

A repository is a place where you can store large piece of quantities. Then whenever you need those quantities you just pick them up and use it.

Photo by Jason Pofahl on Unsplash

Repository Pattern in Computer Science

In Computer Science, you can use Repository Pattern to store your data. Repository Pattern grants you the flexibility to separate your needs. It splits the layers of your program. You can decouple your business logic and data layer(Separation of Concerns). By using this approach, your app structure can be more testable and maintainable.

Use Case

Let’s see how we can use Repository Pattern with RxSwift and MVVM in an iOS Application. Below, you can take a look at the App Architecture. Of course this is not a complete architecture, but when it comes to create a real app you can use this approach to create a mechanism to make your life easier. It is better to use it whenever you need to work both with local data and remote data.

Structure of App

Code

Gateway Layer

Repository class is a generic open class where you can inherit with your concrete repository classes. The generic parameter refers to the type of your data you want to access. A repository can be initialized with an another repository to be able to transfer the data between another repository.

WeatherRemoteRepository is a concrete Repository class. It is responsible for handling the remote data. And also If any WeatherLocalRepository is injected into it. It can calls it save method in order to save the WeatherData locally.

WeatherLocalRepository is another concrete class. It uses a local data source to maintain the data. And also keep in mind that you can always add extra methods to your concrete repositories rather than just using load and save methods.

Data & Networking Layer

WeatherApi can use URLSession or Alamofire to make network requests to gather the data from remote. WeatherRemoteRepository use this class to work with the remote data.

WeatherLocalDataSource is responsible for storing the data via Realm of UserDefaults. WeatherLocalRepository use this class to work with local data.

Presentation Layer

WeatherViewModel holds references of repositories and communicates with the WeatherViewController when needed.

Finally, in your view controller you can update your UI with the fetched weather data. Here is how data flows through the layers:

Data Source -> Repository -> ViewModel -> ViewController

Recap

Repository Pattern is highly useful when you need to work parallel with different data sources such as Remote, Local and Cache. It is also architecturally useful when it comes to Separation of Concerns Principle. It makes your program testable and flexible.

--

--

Oguz Parlak
The Startup

Senior iOS Developer at Raiffeisen Bank International