Last active
March 17, 2023 05:38
-
-
Save satishbabariya/d33473809326a73a586f195fe7cc4972 to your computer and use it in GitHub Desktop.
Simple RxSwift API Call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getTodos() -> Observable<Any> { | |
// If No Internet Connection Throws error | |
// if Reachability()!.connection == .none { | |
// // !!!! return data form cache here | |
// return Observable.error(RESTError(message: "No internet connection.", type: .warning)) | |
// } | |
return Observable.create { (observer) -> Disposable in | |
Alamofire.request("https://jsonplaceholder.typicode.com/todos", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: ["Content-Type": "application/json"]) | |
.responseData { response in | |
switch response.result { | |
case let .success(data): | |
do { | |
observer.onNext(data) | |
observer.onCompleted() | |
} catch { | |
observer.onError(error) | |
} | |
case let .failure(error): | |
observer.onError(error) | |
} | |
} | |
return Disposables.create() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Function call: