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.

Overview

RxAlamoRecord

Version Language: Swift Platform License

Written in Swift 5

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.

Requirements

  • iOS 10.0+ / macOS 10.12+ / tvOS 10.0+ / watchOS 3.0+
  • Xcode 10.2+
  • Swift 5.1

Installation

RxAlamoRecord is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'RxAlamoRecord'

Getting Started

RxAlamoRecord can not be used without first understanding the basic principles of AlamoRecord and RxSwift. It is suggested you have a basic understanding of how both libraries work before proceeding.

Purpose

RxAlamoRecord gives you the power to bind your API reponses directly to AlamoRecordRelay and Action objects. Action is a powerful extension library created by the RxSwiftCommunity

AlamoRecordRelay

This object behaves exactly the same as a BehaviorRelay except that an AlamoRecordRelay can emit an error. It is required to use an instance of an AlamoRecordRelay when binding via RxAlamoRecord or else your app will crash if the API request returns an error or fails.

To help simplify the examples, assume these variables are included in each one:
let posts = AlamoRecordRelay<[Post]>(value: [])
let post = AlamoRecordRelay<Post?>(value: nil)

lazy var failureAction: Action<ApplicationError, Swift.Never> = {
    return Action { [weak self] error in
        // Do something with the error
        return Observable.empty()
    }
}()

Getting all instances of Post

GET https://jsonplaceholder.typicode.com/posts

Post.rx
    .all() 
    .execute()
    .bind(to: posts, failure: failureAction)
    .disposed(by: disposeBag)

Creating an instance of Post

POST https://jsonplaceholder.typicode.com/posts

Post.rx
    .create()
    .withParameters(["userId": userId, "title": title, "body": body]) 
    .execute()
    .bind(to: post, failure: failureAction)
    .disposed(by: disposeBag)

Finding an instance of Post

GET https://jsonplaceholder.typicode.com/posts/1

Post.rx
    .find(id: 1)
    .execute()
    .bind(to: post, failure: failureAction)
    .disposed(by: disposeBag)

Updating an instance of Post

PUT https://jsonplaceholder.typicode.com/posts/1

post.value?
    .rx
    .update()
    .withParameters(["userId": userId, "title": title, "body": body])
    .execute()
    .bind(to: post, failure: failureAction)
    .disposed(by: disposeBag)

This can also be done at the class level:

Post.rx
    .update(id: 1)
    .withParameters(["userId": userId, "title": title, "body": body])
    .execute()
    .bind(to: post, failure: failureAction)
    .disposed(by: disposeBag)

Destroying an instance of Post

DELETE https://jsonplaceholder.typicode.com/posts/1

lazy var destroyedAction: Action<Void, Swift.Never> = {
    return Action { [weak self] in
        // The post is now destroyed
	return Observable.empty()
    }
}()

post.value?
    .rx	
    .destroy()
    .execute()
    .bind(to: destroyedAction, failure: failureAction)
    .disposed(by: disposeBag)

This can also be done at the class level:

Post.rx
    .destroy(id: 1)
    .execute()
    .bind(to: destroyedAction, failure: failureAction)
    .disposed(by: disposeBag)

Assigning default value on failure

It is also possible to assign a default value to an AlamoRecordRelay/Action object if an API request fails:

let postTitle = AlamoRecordRelay<String?>(value: nil)

Post.rx
    .find(id: 1)
    .execute()
    .map { $0.title }
    .bind(to: postTitle, valueOnFailure: "Default Title")
    .disposed(by: disposeBag)
lazy var postTitleAction: Action<String, Swift.Never> = {
    return Action { [weak self] title in
        // Do something with the title
        return Observable.empty()
    }
}()

Post.rx
    .find(id: 1)
    .execute()
    .map { $0.title }
    .bind(to: postTitleAction, valueOnFailure: "Default Title")
    .disposed(by: disposeBag)

Download the example project to see just how easy creating an application with a reactive networking layer is when using RxAlamoRecord!

Author

Dalton Hinterscher, [email protected]

Credits

Reactive Logo used in header image designed by ReactiveX group

License

RxAlamoRecord is available under the MIT license. See the LICENSE file for more info.

You might also like...
Handy RxSwift extensions on NSObject, including rx.disposeBag.
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 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

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

Dynamic and type-safe framework for building linear and non-linear flows.

FlowKit FlowKit is a dynamic flow framework capable of building a flow, based on conditions and ordered according to a logic of next steps. By using F

Cocoa framework and Obj-C dynamism bindings for ReactiveSwift.
Cocoa framework and Obj-C dynamism bindings for ReactiveSwift.

Reactive extensions to Cocoa frameworks, built on top of ReactiveSwift. ⚠️ Looking for the Objective-C API? 🎉 Migrating from RAC 4.x? 🚄 Release Road

Simple and lightweight Functional Reactive Coding in Swift for the rest of us
Simple and lightweight Functional Reactive Coding in Swift for the rest of us

The simplest ObservableT implementation for Functional Reactive Programming you will ever find. This library does not use the term FRP (Functional R

Lightweight observations and bindings in Swift
Lightweight observations and bindings in Swift

What is Hanson? Hanson is a simple, lightweight library to observe and bind values in Swift. It's been developed to support the MVVM architecture in o

A configurable api client based on Alamofire4 and RxSwift4 for iOS

SimpleApiClient A configurable api client based on Alamofire4 and RxSwift4 for iOS Requirements iOS 8.0+ Swift 4 Table of Contents Basic Usage Unwrap

Releases(1.4.0)
Owner
Dalton Hinterscher
Blessed husband and father who loves everything about iOS.
Dalton Hinterscher
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
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

STDev 6 Mar 26, 2021
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 + 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

Yasuhiro Inami 719 Nov 19, 2022
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 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 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