Skip to content

Instantly share code, notes, and snippets.

@minsOne
Created May 15, 2016 14:23
Show Gist options
  • Save minsOne/fddfa60bc13989bfd15707894f5d69b2 to your computer and use it in GitHub Desktop.
Save minsOne/fddfa60bc13989bfd15707894f5d69b2 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// NetworkChaining
//
// Created by JungMin Ahn on 2016. 5. 15..
// Copyright © 2016년 SmartStudy. All rights reserved.
//
import UIKit
import RxSwift
import RxCocoa
extension Dictionary where Key:StringLiteralConvertible, Value: StringLiteralConvertible {
func toParameterString() -> String {
guard let _self = (self as? AnyObject) as? Dictionary<String, String> else {
return ""
}
if _self.count < 10 {
return (1..._self.count+1).map { "\($0)=\($0)" }.joinWithSeparator("&")
}
return (1..._self.count).map { "\($0)=\($0)" }.joinWithSeparator("&")
}
}
protocol HTTPBinAPI {
func get(parameter: String) -> Observable<[String:String]>
}
class HTTPBinDefaultAPI: HTTPBinAPI {
let url = "http://httpbin.org/"
static let sharedAPI = HTTPBinDefaultAPI()
func get(parameter: String) -> Observable<[String : String]> {
let requestURL = NSURL(string: url + "get?" + parameter)!
return Observable.create { observer -> Disposable in
let s = NSURLSession.sharedSession().dataTaskWithURL(requestURL) {
(data, response, error) in
if let error = error {
observer.onError(error)
}
guard let
data = data,
json = try? NSJSONSerialization.JSONObjectWithData(data, options: []),
args = json["args"] as? [String:String]
else {
observer.onError(NSError(domain: "Error", code: 1, userInfo: nil))
return
}
observer.onNext(args)
}
s.resume()
return NopDisposable.instance
}
}
}
class ViewController: UIViewController {
var label: UILabel!
var disposeBag = DisposeBag()
let backgroundScheduler = SerialDispatchQueueScheduler(globalConcurrentQueueQOS: .Background)
override func viewDidLoad() {
super.viewDidLoad()
let btn1 = UIButton(frame: CGRectMake(100,100,100,100))
btn1.backgroundColor = .redColor()
self.view.addSubview(btn1)
let btn2 = UIButton(frame: CGRectMake(250,100,100,100))
btn2.backgroundColor = .greenColor()
self.view.addSubview(btn2)
label = UILabel(frame: CGRectMake(100, 200, 300, 100))
label.backgroundColor = .whiteColor()
label.font = UIFont.systemFontOfSize(15)
self.view.addSubview(label)
btn1
.rx_tap
.subscribeOn(MainScheduler.instance)
.map { [String:String]() }
.debug()
.doOnNext { [unowned self] _ in
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
self.label.text = "Loading..."
}
.debug()
.observeOn(backgroundScheduler)
.flatMapLatest { p in
HTTPBinDefaultAPI.sharedAPI.get(p.toParameterString()).retry(2)
.observeOn(MainScheduler.instance).doOnNext { [unowned self] p in self.label.text = p.description
}
}
.debug()
.flatMapLatest { p in
HTTPBinDefaultAPI.sharedAPI.get(p.toParameterString()).retry(2)
.observeOn(MainScheduler.instance).doOnNext { [unowned self] p in self.label.text = p.description
}
}
.debug()
.flatMapLatest { p in
HTTPBinDefaultAPI.sharedAPI.get(p.toParameterString()).retry(2)
.observeOn(MainScheduler.instance).doOnNext { [unowned self] p in self.label.text = p.description
}
}
.debug()
.flatMapLatest { p in
HTTPBinDefaultAPI.sharedAPI.get(p.toParameterString()).retry(2)
.observeOn(MainScheduler.instance).doOnNext { [unowned self] p in self.label.text = p.description
}
}
.debug()
.flatMapLatest { p in
HTTPBinDefaultAPI.sharedAPI.get(p.toParameterString()).retry(2)
.observeOn(MainScheduler.instance).doOnNext { [unowned self] p in self.label.text = p.description
}
}
.flatMapLatest { p in
HTTPBinDefaultAPI.sharedAPI.get(p.toParameterString()).retry(2)
.observeOn(MainScheduler.instance).doOnNext { [unowned self] p in self.label.text = p.description
}
}
.flatMapLatest { p in
HTTPBinDefaultAPI.sharedAPI.get(p.toParameterString()).retry(2)
.observeOn(MainScheduler.instance).doOnNext { [unowned self] p in self.label.text = p.description
}
}
.debug()
.flatMapLatest { p in
HTTPBinDefaultAPI.sharedAPI.get(p.toParameterString()).retry(2)
.observeOn(MainScheduler.instance)
.doOnNext { [unowned self] p in
self.label.text = p.description
}
}
.debug()
.observeOn(MainScheduler.instance)
.subscribe { [unowned self] s in
guard let e = s.element else {
self.label.text = ""
return
}
self.label.text = "Done"
}
.addDisposableTo(disposeBag)
btn2
.rx_tap
.debug()
.subscribe { [unowned self] _ in
print(self.label.text)
self.label.text = ""
}
.addDisposableTo(disposeBag)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment