【问题标题】:RxSwift - Concat only on conditionRxSwift - 仅在条件下连接
【发布时间】:2018-08-14 00:20:56
【问题描述】:

我有 2 个正在连接的流。如果第一个流执行onError 而不是onComplete,我不应该连接第二个流。

示例:

func updateEntity(entities: [Member]) -> Observable<[Result<Member>]> {
     let remoteUpdate = remoteStore.update(entities: entities)

     return remoteUpdate.concat(localStore.update(entities: entities))
}

如果remoteUpdate 抛出错误,我不应该更新localStore,在remoteUpdate 中调用onError

更新:

public override func update(entities: [PlaceAlert]) -> Observable<[Result<PlaceAlert>]> { 
    let remoteUpdate = remoteStore.update(entities: entities) 
    var entityPlaceHolder: [PlaceAlert] = entities 

    return remoteUpdate.catchError { _ in 
      entityPlaceHolder = [] 
      return localStore.update(entities: entityPlaceHolder)
    }.concat(localStore.update(entities: entityPlaceHolder)) 
} 

只是尝试即兴创作。这会有什么不同吗?如果出现错误,带有 emptyArray 的 LocalUpdate 不会执行任何操作

【问题讨论】:

    标签: swift rx-swift reactive-cocoa


    【解决方案1】:

    concat 不是 if/then 语句。两个updates 都在同一个范围内被调用,只是如果 remoteStore 出错,结果不会反映 localStore.update 响应。

    catchError 是您在这种情况下想要的:

    func updateEntity(entities: [Member]) -> Observable<[Result<Member>]> {
        let remoteUpdate = remoteStore.update(entities: entities)
        return remoteUpdate.catchError { _ in
            localStore.update(entities: entities)
        }
    }
    

    【讨论】:

    • 不错。只有当remoteStore 更新成功并且出错时,我才想更新localStore,只要返回remoteStore 更新的Result
    • 在问题中你说你想要那个。
    猜你喜欢
    • 2020-05-16
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多