Xcode 11 beta 5 中止陷阱:在 @Published 变量上调用接收器时出现 6 错误

Xcode 11 beta 5 Abort trap: 6 error when calling sink on @Published variable

我有一个 @Published 变量来指示我的存储库之一中的用户状态 类。

@Published public var state: UserState = .initial

在测试中,我使用 sink 轻松观察变化并满足我的测试所等待的期望。

原来的测试代码是这样的:

sub = Authentication.shared.$state.receive(on: DispatchQueue.main).sink(receiveValue: { state in
    expectation.fulfill()
})
Authentication.shared.login(with: Credentials(email: "gujci@gmail.com", password: "asdasd"))

这导致编译器错误如下 Abort trap: 6

Global is external, but doesn't have external or weak linkage!
i64* @"$s14TestRepository14AuthenticationC6_state33_B23F0E9C543FDF10733C02EF2F1E18CCLL7Combine9PublishedVyAA9UserStateOGvpWvd"
<unknown>:0: error: fatal error encountered during compilation; please file a bug report with your project and the crash log
<unknown>:0: note: Broken module found, compilation aborted!
Stack dump:
...

发生这种情况的项目与应用程序位于单独的 swift 包中。

当我尝试从应用程序中使用它时(没有通过测试),结果是一样的。另外,我尝试过其他出版商,例如。 JustFuture 从网络调用返回,它们在 sink.

下都工作正常

我从 beta 4 开始就遇到了这个问题,但在之前的版本中从未尝试过,可能也存在于这些版本中。我注意到 Combine 在 beta 4 和 5 之间发生了巨大变化,但错误仍然存​​在。

有没有人设法在外部库的 @Published 变量上使用 sink 函数而没有出现此类错误?

如果我误解了什么,请纠正我,现在我不得不假设,问题出在苹果这边。

最后我通过省略@Published关键字解决了这个问题,直接定义了一个发布者

public private(set) var state: CurrentValueSubject<UserState, Never> = .init(.initial)

可以类似使用。

这让我想到,@Published 只适合与 SwiftUI 交互,更深一层我应该使用来自 Combine 的显式发布者。有什么想法吗?