3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RxSwiftのコールド備忘録1

Posted at

はじめに

RxSwiftのHot・Coldについてなかなか覚えられないので、アウトプットして覚えていくことにしました。
備忘録としてなので、あまり参考にならないかと思います。悪しからず。

RxSwiftにおけるHot・Coldとは?

RxSwiftにおけるObservableにはHot/Coldという性質があります。

Hot

subscribeしなくてもストリームが流れ出る。いわゆる垂れ流し状態。

Cold

subscribeしないとストリームが流れない。手動操作(subscribeが必要)の状態。

Coldの簡単な例

これは何が出力されるでしょうか?

let observable = Observable.of(1,2,3,4,5,6,7)
                  .map { value in
                     print(value)
                  }

パッと見ですと 1,2,3,4,5,6,7が出力されそうですが、

実はこれは何も流れません。
mapを含め、ほとんどのオペレータはコールドなので、subscribeするまでストリームは流れません。

observable
     .subscribe(onNext: { value in
           print(value)
     })
     .disposed(by: disposeBag)

subscribeすることでストリームは流れます。

当たり前ですが、とても重要なのでまずはColdについての備忘録でした。
Hotについてはまたの機会に。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?