A RxSwift helper for network calls, JSON decoding and image decoding
Summary: We propose a small helper in order to perform some classical asynchronous operations in RxSwift
like: loading some Data
with URLSession
, decoding these Data
onto some Decodable
element or decoding these Data
onto an UIImage
.
RxSwift
RxSwift is ReactiveX for Swift.
More info here: https://github.com/ReactiveX/RxSwift
Description
We propose a small helper in order to perform some classical operations in RxSwift
like: loading some Data
with URLSession
, decoding these Data
onto some Decodable
element or decoding these Data
onto an UIImage
.
These operations can benefit from being performed asynchronously with the help of RxSwift.
The helper is made of 3 building blocks:
URLSession.download
: takes anURL
and produce aSingle<Data>
DecodeHelper.decode
: takes someData
and produce aSingle<T>
whereT
conforms toDecodable
ImageHelper.decode
: takes someData
and produce aSingle<UIImage>
These building blocks can be combined with flatMap
, like below :
class RandomUserService: RandomUserServiceProtocol {
func contentObservable() -> Single<RandomUserAPI.Content> {
return URLSession.shared.download(url: RandomUserAPI.url)
.flatMap( DecodeHelper.decode )
}
}
URLSession+RxSwift.swift
This extension will help for doing network calls with RxSwift.
DecodeHelper.swift
This module will help for doing JSON decode in RxSwift.
ImageHelper.swift
This will help for doing image decode with RxSwift.