gpt4 book ai didi

Swift 组合 : What are those multicast functions for and how do I use them?

转载 作者:行者123 更新时间:2023-12-01 03:06:24 26 4
gpt4 key购买 nike

我在 https://developer.apple.com/documentation/combine/publisher 中的“与多个订阅者合作”部分遇到了一些合并问题。 :

func multicast<S>(() -> S) -> Publishers.Multicast<Self, S>

func multicast<S>(subject: S) -> Publishers.Multicast<Self, S>

但是,当我试图确认我的假设是,当发送给多个订阅者时需要多播,我发现在尝试这个操场代码(修改自 https://github.com/AvdLee/CombineSwiftPlayground/blob/master/Combine.playground/Pages/Combining%20Publishers.xcplaygroundpage/Contents.swift )(在 Xcode 版本 11.0 中的 10.14.5 上运行时,这不是必需的测试版 3 (11M362v)):

enum FormError: Error { }

let usernamePublisher = PassthroughSubject<String, FormError>()
let passwordPublisher = PassthroughSubject<String, FormError>()

let validatedCredentials = Publishers.CombineLatest(usernamePublisher, passwordPublisher)
.map { (username, password) -> (String, String) in
return (username, password)
}
.map { (username, password) -> Bool in
!username.isEmpty && !password.isEmpty && password.count > 12
}
.eraseToAnyPublisher()

let firstSubscriber = validatedCredentials.sink { (valid) in
print("First Subscriber: CombineLatest: Are the credentials valid: \(valid)")
}

let secondSubscriber = validatedCredentials.sink { (valid) in
print("Second Subscriber: CombineLatest: Are the credentials valid: \(valid)")
}

// Nothing will be printed yet as `CombineLatest` requires both publishers to have send at least one value.
usernamePublisher.send("avanderlee")
passwordPublisher.send("weakpass")
passwordPublisher.send("verystrongpassword")

这打印:
First Subscriber: CombineLatest: Are the credentials valid: false
Second Subscriber: CombineLatest: Are the credentials valid: false
First Subscriber: CombineLatest: Are the credentials valid: true
Second Subscriber: CombineLatest: Are the credentials valid: true

所以似乎不需要多播来寻址多个订阅者。还是我错了?

那么,这些多播功能的用途是什么,我将如何使用它们?一些示例代码会很好。

谢谢,

拉斯

最佳答案

PassthroughSubject 不是一个很好的测试示例,因为它是一个类并为您提供引用语义。因此,在一个简单的情况下,两个订阅者可以直接订阅它并在主体发出一个值时同时接收相同的值。

但这里有一个更好的测试用例(灵感来自关于 Cocoa With Love 的讨论):

    let pub1 = Timer.publish(every: 1, on: .main, in: .default).autoconnect()
let sub = CurrentValueSubject<Int,Never>(0)
let scan = sub.scan(10) {i,j in i+j}
pub1.sink { _ in let i = sub.value; sub.value = i+1 }.store(in:&storage)
scan.sink { print("a", $0) }.store(in:&storage)
delay(3) {
scan.sink { print("b", $0) }.store(in:&self.storage)
}

当第二个 sink 时,这给出了一个非常奇怪的结果。作为此管道的新订阅者出现:
a 10
a 11
a 13
a 16
b 13
a 20
b 17
a 25
b 22
a 31
b 28
a 38
b 35

水槽 ab正在获得彼此不同的一系列数字,实际上是因为 scan是一个结构。如果我们希望它们获得相同的数字,我们可以使用多播:
    let scan = sub.scan(10) {i,j in i+j}.multicast {PassthroughSubject()}.autoconnect()

这产生
a 10
a 11
a 13
a 16
a 20
b 20
a 25
b 25

这是连贯的。

但是,这仍然不能证明您需要 multicast ,因为你可以通过说 .share() 来完成同样的事情。反而。我不清楚 multicast 之间的区别是什么和 share .

关于Swift 组合 : What are those multicast functions for and how do I use them?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56918286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com