flatmapを使用して切り替わるストリームのエラーは、結合フレームワークの上流に関係しません.
エラーの場合、発行元はストリームを終了します.しかし、Publisherがflatmapによって切り換えられるならば、スイッチの前の上流は終了しません.
(これはrxswiftとは大きな違いです)
フラットマップサンプルの使用
PasshTagersubject & flatMapサンプルの使用
もちろん、Passthroughの使用は結果を変更しません.
私の推測では、このためには、サブスクリプションでそれを制御する必要があります.
(これはrxswiftとは大きな違いです)
フラットマップサンプルの使用
import Foundation
import Combine
let cancelable = (1...3).publisher
.handleEvents(receiveOutput: {
print("🥷 handle output: \($0)")
}, receiveCompletion: { _ in
print("🥷 handle completion:")
}, receiveCancel: {
print("🥷 handle cancel:")
})
.setFailureType(to: NSError.self)
.flatMap { value -> Future<String, NSError> in
print("🍏 flatMap: \(value)")
return Future<String, NSError> {
guard value == 1 else {
$0(.failure(NSError(domain: "test", code: 1)))
return
}
$0(.success("\(value)"))
}
}
.sink(receiveCompletion: {
switch $0 {
case .finished:
print("🍎 sink finished:")
case .failure(let error):
print("🍎 sink failure:", error)
}
}, receiveValue: {
print("🍎 sink received: \(String(describing: $0)) 🎉")
})
このコードには次の出力があります.🥷 handle output: 1
🍏 flatMap: 1
🍎 sink received: 1 🎉
🥷 handle output: 2
🍏 flatMap: 2
🍎 sink failure: Error Domain=test Code=1 "(null)"
🥷 handle output: 3
🥷 handle completion:
注意🥷 絵文字シンク故障にもかかわらず、出力は、要素が3で扱われて、完成まで送られるのを見ます.PasshTagersubject & flatMapサンプルの使用
もちろん、Passthroughの使用は結果を変更しません.
import Foundation
import Combine
let subject = PassthroughSubject<Int, NSError>()
let cancelable = subject
.handleEvents(receiveOutput: {
print("🥷 handle output: \($0)")
}, receiveCompletion: { _ in
print("🥷 handle completion:")
}, receiveCancel: {
print("🥷 handle cancel:")
})
.flatMap { value -> Future<String, NSError> in
print("🍏 flatMap: \(value)")
return .init {
guard value == 1 else {
$0(.failure(NSError(domain: "test", code: 1)))
return
}
$0(.success("\(value)"))
}
}
.sink(receiveCompletion: {
switch $0 {
case .finished:
print("🍎 sink finished:")
case .failure(let error):
print("🍎 sink failure:", error)
}
}, receiveValue: {
print("🍎 sink received: \(String(describing: $0)) 🎉")
})
subject.send(1)
subject.send(2)
subject.send(3)
subject.send(completion: .finished)
このコードには次の出力があります.🥷 handle output: 1
🍏 flatMap: 1
🍎 sink received: 1 🎉
🥷 handle output: 2
🍏 flatMap: 2
🍎 sink failure: Error Domain=test Code=1 "(null)"
🥷 handle output: 3
🥷 handle completion:
結局、川下スイッチトストリームが失敗するなら、上流は気にかけません.これにより、ストリーム実行プロセスが不必要に継続します.私の推測では、このためには、サブスクリプションでそれを制御する必要があります.
Reference
この問題について(flatmapを使用して切り替わるストリームのエラーは、結合フレームワークの上流に関係しません.), 我々は、より多くの情報をここで見つけました https://dev.to/yimajo/errors-in-the-stream-switched-using-flatmap-are-not-involved-upstream-in-combine-framework-5d92テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol