diff --git a/RxSwift/Observables/Multicast.swift b/RxSwift/Observables/Multicast.swift index 279291c4b..ccbcb4510 100644 --- a/RxSwift/Observables/Multicast.swift +++ b/RxSwift/Observables/Multicast.swift @@ -124,6 +124,9 @@ final fileprivate class RefCountSink Disposable { let subscription = _parent._source.subscribe(self) + if self.disposed { + return Disposables.create() + } _parent._lock.lock(); defer { _parent._lock.unlock() } // { if _parent._count == 0 { diff --git a/Tests/RxSwiftTests/Observable+ShareReplay1Tests.swift b/Tests/RxSwiftTests/Observable+ShareReplay1Tests.swift index 6049c69d8..d2b1d9895 100644 --- a/Tests/RxSwiftTests/Observable+ShareReplay1Tests.swift +++ b/Tests/RxSwiftTests/Observable+ShareReplay1Tests.swift @@ -205,14 +205,7 @@ extension ObservableShareReplay1Test { ]) // unoptimized version of replay subject will make a subscription and kill it immediately - XCTAssertEqual(xs.subscriptions[0], Subscription(335, 365)) - - switch version { - case .composition: - XCTAssertTrue(xs.subscriptions.count == 2 && xs.subscriptions[1] == Subscription(440, 440)) - case .optimized: - XCTAssertTrue(xs.subscriptions.count == 1) - } + XCTAssertEqual(xs.subscriptions, [Subscription(335, 365)]) } } @@ -275,15 +268,7 @@ extension ObservableShareReplay1Test { completed(365) ]) - XCTAssertEqual(xs.subscriptions[0], Subscription(335, 365)) - - // unoptimized version of replay subject will make a subscription and kill it immediately - switch version { - case .composition: - XCTAssertTrue(xs.subscriptions.count == 2 && xs.subscriptions[1] == Subscription(440, 440)) - case .optimized: - XCTAssertTrue(xs.subscriptions.count == 1) - } + XCTAssertEqual(xs.subscriptions, [Subscription(335, 365)]) } } @@ -401,18 +386,10 @@ extension ObservableShareReplay1Test { completed(365) ]) - XCTAssertEqual(xs.subscriptions[0], Subscription(335, 365)) - - // unoptimized version of replay subject will make a subscription and kill it immediately - switch version { - case .composition: - XCTAssertTrue(xs.subscriptions.count == 2 && xs.subscriptions[1] == Subscription(440, 440)) - case .optimized: - XCTAssertTrue(xs.subscriptions.count == 1) - } + XCTAssertEqual(xs.subscriptions, [Subscription(335, 365)]) } } - + #if TRACE_RESOURCES func testShareReplayReleasesResourcesOnComplete() { _ = Observable.just(1).shareReplay(1).subscribe()