Last active
September 25, 2016 16:42
-
-
Save yota345/161420cbd38c5da9f563f7be2975cc3d to your computer and use it in GitHub Desktop.
EventsModel (https://github.com/takuwan0405/RxSwiftSample)
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
/** | |
Eventモデル | |
- requestState: API通信の状態を扱う | |
- disposeBag: RxSwiftで不要なstreamを削除するためのクラス | |
*/ | |
struct EventsModel { | |
internal let requestState = Variable(RequestState.Stopped) | |
private let disposeBag = DisposeBag() | |
} | |
extension EventsModel { | |
/** | |
- Eventの一覧を取得する | |
- APIレスポンスは、self.requestStateに格納 | |
*/ | |
func fetchEventList(count: Int) { | |
requestState.value = .Requesting | |
EventsRequest | |
.GetEvents(count) | |
.request() | |
.map{ try! decodeValue($0) as EventListResponse } | |
.subscribe( | |
onNext: { | |
self.requestState.value = .Response($0) | |
}, onError: { | |
self.requestState.value = .Error($0) | |
} | |
) | |
.addDisposableTo(disposeBag) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment