Alamofire API Request by RxSwift
Alamofire API Request by RxSwift
pod 'RxCocoa'
pod 'RxSwift'
pod 'Alamofire'
- Download and drop
Source
folder with files in your project. - Add your API end point URL in your project.
- Congratulations!
RRAlamofireRxAPI is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'RRAlamofireRxAPI', '~> 2.0.0'
In Xcode, use the menu File > Swift Packages > Add Package Dependency... and enter the package URL https://github.com/Jigneshmayani90/AlamofireRxAPI
.
To run the example project, clone the repo, and run pod install
from the Example directory first.
To run the example project, clone the repo, and run pod install from the Example directory first.
/// Uses
let createCustomSession = Session()
let request = RRAPIRxManager.shared
.setSessionManager(createCustomSession) //`Session` creates and manages Alamofire's `Request` types during their lifetimes.
.setHttpMethod(.get) // httpMethod: GET, POST, PUT & DELETE
.setURL("Your API URL")
.setHeaders([:]) // a dictionary of parameters to apply to a `HTTPHeaders`.
.setParameter([:]) // a dictionary of parameters to apply to a `URLRequest`.
request.setDeferredAsObservable()
.subscribeConcurrentBackgroundToMainThreads()
.subscribe { (response) in
/// The response of data type is Data.
/// <#T##Here: decode JSON Data into your custom model structure / class#>
print(response)
} onError: { (error) in
print(error.localizedDescription)
}.disposed(by: rxbag)
/// Example 1
/// Loader start
let userIds = [1, 2, 3]
Observable.from(userIds)
.flatMap { (userId) -> Observable<Any> in
return RRAPIRxManager.shared.setURL("https://jsonplaceholder.typicode.com/users/\(userId)")
.setDeferredAsObservable()
}
.toArray() // collecting all users object data
.asObservable()
.subscribeConcurrentBackgroundToMainThreads()
.subscribe { (response) in
print("Got users:")
/// Loader stop
} onError: { (error) in
print(error)
}.disposed(by: rxbag)
/// Example 2
/// Loader start
RRAPIRxManager.shared.setURL("https://jsonplaceholder.typicode.com/users/1")
.flatMap { response -> Observable<Any> in
guard let data = response as? [String:Any] else { return Observable.empty() }
print(data["username"] ?? "")
return RRAPIRxManager.shared.setURL("https://jsonplaceholder.typicode.com/users/2")
//.delaySubscribeConcurrentBackgroundToMainThreads(.milliseconds(200))
.setDeferredAsObservable()
}
.subscribeConcurrentBackgroundToMainThreads()
.subscribe { (response) in
guard let data = response as? [String:Any] else { return }
print(data["username"] ?? "")
/// Loader stop
} onError: { (error) in
print(error)
}.disposed(by: rxbag)
We would love you for the contribution to AlamofireRxAPI, check the LICENSE
file for more info.
AlamofireRxAPI is available under the MIT license. See the LICENSE file for more info.