【问题标题】:`flatMap` publishers with different Errors具有不同错误的`flatMap`发布者
【发布时间】:2020-08-07 18:23:48
【问题描述】:

我有一个像下面这样的 Swift 发布者

let item = PassthrogutSubject<String, Never>()

收到字符串后我要发起网络请求

为此,我使用flatMap

我的网络请求签名如下:

func loadDetails(_ code: String) -> AnyPublisher<CustomType, ErrorAlert>

我怎样才能做到以下几点:

$item
 .filter { !$0.isEmpty }
 .flatMap { [unowned self] (string) in
    self.viewModel.loadDetails(string) }
 .receive(on: DispatchQueue.main)
 .sink(receiveCompletion: { _ in
     ...
  }) {  ... }
 .store(in: &subscriptions)

错误:

Instance method 'flatMap(maxPublishers:_:)' requires the types 'Published&lt;Value&gt;.Publisher.Failure' (aka 'Never') and 'Alert' be equivalent

【问题讨论】:

    标签: ios swift networking functional-programming combine


    【解决方案1】:

    在收到第一个订阅者时,我用以下操作员解决了我的问题

    $item
     .filter { !$0.isEmpty }
     .setFailureType(to: ErrorAlert.self) // <---- ? HERE
     .flatMap { [unowned self] (string) in
        self.viewModel.loadDetails(string) }
     .receive(on: DispatchQueue.main)
     .sink(receiveCompletion: { _ in
         ...
      }) {  ... }
     .store(in: &subscriptions)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2011-08-09
      相关资源
      最近更新 更多