Skip to content

Commit

Permalink
Fix crash in .publisher() caused by immediately cancelling subscrib…
Browse files Browse the repository at this point in the history
…er (#66)
  • Loading branch information
OCJvanDijk committed Apr 18, 2021
1 parent 63dcea3 commit 6158b9b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 0 additions & 1 deletion Sources/Defaults/Observation+Combine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ extension Defaults {
}

func cancel() {
observation?.invalidate()
observation = nil
subscriber = nil
}
Expand Down
16 changes: 16 additions & 0 deletions Tests/DefaultsTests/DefaultsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<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)
}
}

0 comments on commit 6158b9b

Please sign in to comment.