Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Daltron/RxAlamoRecord
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.
https://github.com/Daltron/RxAlamoRecord
activerecord alamofire api ios json networking reactive-programming rxswift
Last synced: about 1 month ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/Daltron/RxAlamoRecord
- Owner: Daltron
- License: mit
- Created: 2018-08-10T13:49:16.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-07T02:53:25.000Z (over 4 years ago)
- Last Synced: 2024-11-26T02:12:50.915Z (about 2 months ago)
- Topics: activerecord, alamofire, api, ios, json, networking, reactive-programming, rxswift
- Language: Swift
- Size: 1.56 MB
- Stars: 9
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-ios - RxAlamoRecord
- awesome-ios-star - RxAlamoRecord
README
![RxAlamoRecord](https://github.com/Daltron/RxAlamoRecord/blob/master/RxAlamoRecord/Assets/rx_alamorecord.png?raw=true)
[![Version](https://img.shields.io/cocoapods/v/RxAlamoRecord.svg?style=flat)](http://cocoapods.org/pods/RxAlamoRecord)
[![Platform](https://img.shields.io/cocoapods/p/RxAlamoRecord.svg?style=flat)](http://cocoapods.org/pods/RxAlamoRecord)
[![License](https://img.shields.io/cocoapods/l/RxAlamoRecord.svg?style=flat)](http://cocoapods.org/pods/RxAlamoRecord)## Written in Swift 5
RxAlamoRecord combines the power of the [AlamoRecord](https://github.com/tunespeak/AlamoRecord) and [RxSwift](https://github.com/ReactiveX/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](http://cocoapods.org). To install
it, simply add the following line to your Podfile:```ruby
pod 'RxAlamoRecord'
```## Getting Started
RxAlamoRecord can not be used without first understanding the basic principles of [AlamoRecord](https://github.com/tunespeak/AlamoRecord) and [RxSwift](https://github.com/ReactiveX/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](https://github.com/RxSwiftCommunity/Action) is a powerful extension library created by the [RxSwiftCommunity](https://github.com/RxSwiftCommunity)
#### `AlamoRecordRelay`
This object behaves exactly the same as a [BehaviorRelay](https://github.com/ReactiveX/RxSwift/blob/4431b623751ac5525e8a8c2d6e82f29b983af07c/RxCocoa/Traits/BehaviorRelay.swift) 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:
```swift
let posts = AlamoRecordRelay<[Post]>(value: [])
let post = AlamoRecordRelay(value: nil)lazy var failureAction: Action = {
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](https://jsonplaceholder.typicode.com/posts)
```swift
Post.rx
.all()
.execute()
.bind(to: posts, failure: failureAction)
.disposed(by: disposeBag)
```### Creating an instance of `Post`
`POST` [https://jsonplaceholder.typicode.com/posts](https://jsonplaceholder.typicode.com/posts)
```swift
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](https://jsonplaceholder.typicode.com/posts/1)
```swift
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](https://jsonplaceholder.typicode.com/posts/1)
```swift
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:```swift
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](https://jsonplaceholder.typicode.com/posts/1)
```swift
lazy var destroyedAction: Action = {
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:```swift
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:
```swift
let postTitle = AlamoRecordRelay(value: nil)Post.rx
.find(id: 1)
.execute()
.map { $0.title }
.bind(to: postTitle, valueOnFailure: "Default Title")
.disposed(by: disposeBag)
``````swift
lazy var postTitleAction: Action = {
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](https://github.com/ReactiveX) group
## License
RxAlamoRecord is available under the MIT license. See the LICENSE file for more info.