37
18

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のshare*の早見表

Last updated at Posted at 2017-07-27

はじめに

RxSwiftの3.5.0からshare(replay:scope:)が追加されました。
3.5.0未満のshare()shareReplay(_:)shareReplayLatestWhileConnected()にどう対応しているのか書いていきます。

.share()

.share(scope: .forever)

.shareReplay(1)

.share(replay: 1, scope: .forever)

.shareReplayLatestWhileConnected()

.share(replay: 1, scope: .whileConnected)

参考

RxSwift 3.5.0 Release Note

RxSwift/Observables/ShareReplayScope.swift
public func share(replay: Int = 0, scope: SubjectLifetimeScope)
        -> Observable<E> {
    switch scope {
    case .forever:
        switch replay {
        case 0: return self.multicast(PublishSubject()).refCount()
        default: return shareReplay(replay)
        }
    case .whileConnected:
        switch replay {
        case 0: return ShareWhileConnected(source: self.asObservable())
        case 1: return ShareReplay1WhileConnected(source: self.asObservable())
        default: return self.multicast(makeSubject: { ReplaySubject.create(bufferSize: replay) }).refCount()
        }
    }
}

追記

.share()

RxSwift/Observables/Multicast.swift
public func publish() -> ConnectableObservable<E> {
    return self.multicast(PublishSubject())
}

public func share() -> Observable<E> {
    return self.publish().refCount()
}

.shareReplayLatestWhileConnected()

RxSwift/Observables/ShareReplayScope.swift
public func shareReplayLatestWhileConnected() -> Observable<E> {
    return ShareReplay1WhileConnected(source: self.asObservable())
}
37
18
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
37
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?