Realm RxSwift - This application was written in order to use Realm, RxSwift frameworks in real example

Overview

Realm_RxSwift

This simple app was written to introduce basic operations of some frameworks. Such as "Realm" and "RxSwift". Also this app was written with MVVM pattern with Coordinators.

Coordinator

Here we are declaring our functions with protocol.

  protocol AppCoordinatorProtocol {
      var window: UIWindow { get }
      var navigationController: UINavigationController { get }

      func start()
      func startDatasVC(userModel: UserModel, editRelay: PublishRelay<UserModel?>?)
      func showAddUserAlert(_ relay: PublishRelay<String?>)
      func showError(with msg: String)
  }

Here we are initialising our protocol.

  class AppCoordinator: AppCoordinatorProtocol {

      var window: UIWindow
      var navigationController: UINavigationController

      init(window: UIWindow) {
          self.window = window
          self.navigationController = UINavigationController()
          self.window.rootViewController = navigationController
      }

      func start() {
          self.window.makeKeyAndVisible()
          let mainVC = MainVC.instantiate()
          mainVC.appCoordinator = self
          mainVC.mainViewModel = MainViewModel()
          self.navigationController.setViewControllers([mainVC], animated: true)
      }
  ...
  }

Realm(Create-Read-Update)

Create

        let newUser = UserModel()
        newUser.id = UUID().uuidString
        newUser.username = username
        newUser.selected = false

        
        do {
            try db.write {
                self.db.add(newUser)
            }
        } catch {
            //error
        }

Read(Fetching objects)

        if let usersResult = db.objects(UserModel.self) {
            let usersArray: [UserModel] = Array(usersResult)
            print(usersArray)
        } else {
            //error
        }

Update

        do {
            try db.write {
                user.selected = !user.selected
            }
        } catch {
                //error    
        }

RxSwift Traits

Completable

"Completable" is a type of RxSwift traits. "Completable" finishes with 2 functions. First is ".completed", second is ".error(SomeError)". In real use-case, we can use this trait when we need only information about completion or error result. For example when uploading or inserting datas. In this app I also used "Completable" trait in order to creating new UserModel:

  func addUser(username: String) -> Completable {
    return Completable.create { [weak self] observer in
        let maybeError = RealmError(msg: "Realm add user error")
        
        guard let self = self else {
            observer(.error(maybeError))
            return Disposables.create()
        }
        
        let newUser = UserModel()
        newUser.id = UUID().uuidString
        newUser.username = username
        newUser.selected = false
        
        do {
            try self.db?.write {
                self.db?.add(newUser)
                observer(.completed)
            }
        } catch {
            observer(.error(maybeError))
        }

        return Disposables.create {}
    }
}

Single

Second trait type which I used in this application is "Single". In this trait you have 2 chances to finish. First is ".success(SomeData)", second is ".failure(SomeError)". In this app I used this trait in order to fetch users:

  func fetchUsers() -> Single<[UserModel]> {
    return Single<[UserModel]>.create { [weak self] observer in
        let maybeError = RealmError(msg: "Realm fetch error")
        
        guard let self = self else {
            observer(.failure(maybeError))
            return Disposables.create()
        }
        
        if let usersResult = self.db?.objects(UserModel.self) {
            let usersArray: [UserModel] = Array(usersResult)
            observer(.success(usersArray))
        } else {
            observer(.failure(maybeError))
        }
        
        return Disposables.create {}
    }
}

Driver

Third trait which I want to introduce is "Driver". This trait is to work with UI. Because this trait works on MainThread. For instance, I used this trait when user opens alert dialog in order to add user and clicks "save" button:

  ...
  let alertRelay = PublishRelay<String?>()
  ...
  
     alertRelay
        .asDriver(onErrorJustReturn: "")
        .map { ($0 ?? "") }
        .filter { $0 != "" }
        .drive { [weak self] text in
            self?.addUser(text)
        }
        .disposed(by: bag)
  ...
You might also like...
🤖 RxSwift + State Machine, inspired by Redux and Elm.
🤖 RxSwift + State Machine, inspired by Redux and Elm.

RxAutomaton RxSwift port of ReactiveAutomaton (State Machine). Terminology Whenever the word "signal" or "(signal) producer" appears (derived from Rea

STDevRxExt contains some extension functions for RxSwift and RxCocoa which makes our live easy.

STDevRxExt Example To run the Example.playground, clone the repo, and run pod install from the Example directory first. Requirements iOS 9.0+ tvOS 9.0

RxAlamoRecord combines the power of the AlamoRecord and RxSwift libraries to create a networking layer that makes interacting with API's easier than ever reactively.
RxAlamoRecord combines the power of the AlamoRecord and RxSwift libraries to create a networking layer that makes interacting with API's easier than ever reactively.

Written in Swift 5 RxAlamoRecord combines the power of the AlamoRecord and RxSwift libraries to create a networking layer that makes interacting with

Support Combine Assign subscriber in RxSwift.

RxAssign Support Combine Assign subscriber in RxSwift. Assign uses a KeyPath which is really nice and useful. ** RxAssign extends Driver and Signal in

This is the demo of MVVM-C structure with dependency injection using RxSwift.
This is the demo of MVVM-C structure with dependency injection using RxSwift.

MVVMC-Demo Demo defination This is the demo project, I have integrated two APIs for MovieDB APIS (https://www.themoviedb.org/). One for the listing of

CMPSC475 Final Project, ArboretumID Application allows users to explore the Penn State Arboretum, identify plants and learn about the exhibits!

ArboretumID: CMPSC475 Final Project Taylan Unal (@taylanu) About ArboretumID ArboretumIdentifier (ArboretumID) is an app that enhances the Penn State

Sample iOS application in SwiftUI presenting Redux architecture
Sample iOS application in SwiftUI presenting Redux architecture

SwiftUI-Redux-Demo Sample iOS application in SwiftUI presenting Redux architecture. My full article about Redux in detail you will find here: Redux ar

Eazy is the missing piece in your SwiftUI and UIKit application.

Eazy is the missing piece in your SwiftUI and UIKit application. It aims at harmonizing how your views communicate with the model and vice versa in a clear and consistent way. Eazy can be used on any Apple platform.

Binding - Data binding framework (view model binding on MVVM) written using propertyWrapper and resultBuilder

Binding Data binding framework (view model binding on MVVM) written using @prope

Owner
Elbek Khasanov
21 y.o iOS developer
Elbek Khasanov
Super Simple Pager with RxSwift extension

SSPager Super Simple Pager Example To run the example project, clone the repo, and run pod install from the SSPagerExample directory first. Installati

9oya 4 Jul 10, 2022
RxSwift extentions for Swift optionals and "Occupiable" types

RxOptional RxSwift extentions for Swift optionals and "Occupiable" types. Usage All operators are available on Driver as well unless otherwise marked.

Thane Gill 8 Jun 28, 2020
RxSwift bindings for Permissions API in iOS.

RxPermission RxSwift bindings for Permission API that helps you with Permissions in iOS. Installation RxPermission is available through CocoaPods. I c

Luke 230 Dec 27, 2022
RxSwift wrapper around the elegant HTTP networking in Swift Alamofire

RxAlamofire RxAlamofire is a RxSwift wrapper around the elegant HTTP networking in Swift Alamofire. Getting Started Wrapping RxSwift around Alamofire

RxSwift Community 1.6k Jan 3, 2023
RxSwift extension for RealmSwift's types

RxRealm This library is a thin wrapper around RealmSwift ( Realm Docs ). Table of contents: Observing object collections Observing a single object Wri

RxSwift Community 1.1k Jan 6, 2023
A testable RxSwift wrapper around MultipeerConnectivity

A testable RxSwift wrapper around MultipeerConnectivity RxMultipeer is a RxSwift wrapper for MultipeerConnectivity. Using the adapter pattern, we can

RxSwift Community 69 Jul 5, 2022
iOS & OSX Bluetooth library for RxSwift

RxBluetoothKit is a Bluetooth library that makes interaction with BLE devices much more pleasant. It's backed by RxSwift and CoreBluetooth and it prov

Polidea 1.3k Dec 16, 2022
RxSwift reactive wrapper for view gestures

RxGesture Usage To run the example project, clone the repo, in the Example folder open RxGesture.xcworkspace. You might need to run pod install from t

RxSwift Community 1.3k Dec 30, 2022
Handy RxSwift extensions on NSObject, including rx.disposeBag.

NSObject+Rx If you're using RxSwift, you've probably encountered the following code more than a few times. class MyObject: Whatever { let disposeBag

RxSwift Community 625 Dec 12, 2022
RxSwift extensions for Core Data

RxCoreData Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Xcode 9.0 Swift 4.0

RxSwift Community 164 Oct 14, 2022