Fix crash in `.publisher()` caused by immediately cancelling subscriber (#66)

This commit is contained in:
OCJvanDijk 2021-04-18 18:45:37 +02:00 committed by GitHub
parent 63dcea312f
commit 6158b9bdb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View File

@ -27,7 +27,6 @@ extension Defaults {
} }
func cancel() { func cancel() {
observation?.invalidate()
observation = nil observation = nil
subscriber = nil subscriber = nil
} }

View File

@ -925,4 +925,20 @@ final class DefaultsTests: XCTestCase {
waitForExpectations(timeout: 10) waitForExpectations(timeout: 10)
} }
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
func testImmediatelyFinishingPublisherCombine() {
let key = Defaults.Key<Bool>("observeKey", default: false)
let expect = expectation(description: "Observation closure being called without crashing")
let cancellable = Defaults
.publisher(key, options: [.initial])
.first()
.sink { _ in
expect.fulfill()
}
cancellable.cancel()
waitForExpectations(timeout: 10)
}
} }