From 6158b9bdb3820ab896b2323f37f5cd3b24ea8b27 Mon Sep 17 00:00:00 2001 From: OCJvanDijk Date: Sun, 18 Apr 2021 18:45:37 +0200 Subject: [PATCH] Fix crash in `.publisher()` caused by immediately cancelling subscriber (#66) --- Sources/Defaults/Observation+Combine.swift | 1 - Tests/DefaultsTests/DefaultsTests.swift | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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) + } }