diff --git a/Sources/Defaults/Observation+Combine.swift b/Sources/Defaults/Observation+Combine.swift index c4e6ee9..ca485d2 100644 --- a/Sources/Defaults/Observation+Combine.swift +++ b/Sources/Defaults/Observation+Combine.swift @@ -27,7 +27,6 @@ extension Defaults { } func cancel() { - observation?.invalidate() observation = nil subscriber = nil } diff --git a/Tests/DefaultsTests/DefaultsTests.swift b/Tests/DefaultsTests/DefaultsTests.swift index 5a04656..451bd32 100644 --- a/Tests/DefaultsTests/DefaultsTests.swift +++ b/Tests/DefaultsTests/DefaultsTests.swift @@ -925,4 +925,20 @@ final class DefaultsTests: XCTestCase { 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("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) + } }