docs/Enums/SubjectLifetimeScope.html
public enum SubjectLifetimeScope
Subject lifetime scope
`
whileConnected
`
Each connection will have it’s own subject instance to store replay events.** Connections will be isolated from each another.**
Configures the underlying implementation to behave equivalent to.
source.multicast(makeSubject: { MySubject() }).refCount()
This is the recommended default.
This has the following consequences:
retry or concat operators will function as expected because terminating the sequence will clear internal state.let xs = Observable.deferred { () -> Observable<TimeInterval> in
print("Performing work ...")
return Observable.just(Date().timeIntervalSince1970)
}
.share(replay: 1, scope: .whileConnected)
_ = xs.subscribe(onNext: { print("next \($0)") }, onCompleted: { print("completed\n") })
_ = xs.subscribe(onNext: { print("next \($0)") }, onCompleted: { print("completed\n") })
_ = xs.subscribe(onNext: { print("next \($0)") }, onCompleted: { print("completed\n") })
Notice how time interval is different and Performing work ... is printed each time)
Performing work ...
next 1495998900.82141
completed
Performing work ...
next 1495998900.82359
completed
Performing work ...
next 1495998900.82444
completed
Swift
case whileConnected
`
forever
`
One subject will store replay events for all connections to source.** Connections won’t be isolated from each another.**
Configures the underlying implementation behave equivalent to.
source.multicast(MySubject()).refCount()
This has the following consequences:
retry or concat operators after this operator usually isn’t advised.let xs = Observable.deferred { () -> Observable<TimeInterval> in
print("Performing work ...")
return Observable.just(Date().timeIntervalSince1970)
}
.share(replay: 1, scope: .forever)
_ = xs.subscribe(onNext: { print("next \($0)") }, onCompleted: { print("completed\n") })
_ = xs.subscribe(onNext: { print("next \($0)") }, onCompleted: { print("completed\n") })
_ = xs.subscribe(onNext: { print("next \($0)") }, onCompleted: { print("completed\n") })
Notice how time interval is the same, replayed, and Performing work ... is printed only once
Performing work ...
next 1495999013.76356
completed
next 1495999013.76356
completed
next 1495999013.76356
completed
Swift
case forever