3

我有一个@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 开始就有这个问题,但在以前的版本中从未尝试过,可能也会出现在这些问题中。我注意到,在 beta 4 和 5 之间,Combine 发生了巨大的变化,但是错误仍然存​​在。

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

如果我误解了某些东西,请纠正我,现在我不得不假设,问题出在 Apple 的最后。

4

1 回答 1

3

最后,我@Published通过直接定义发布者来省略关键字来解决它

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

可以类似地使用。

这让我想到,这@Published仅对与 SwiftUI 交互有用,更深一层我应该使用来自Combine. 对此有什么想法吗?

于 2019-08-02T10:50:13.387 回答