gpt4 book ai didi

ios - 合并两个单一响应 RXSwift

转载 作者:行者123 更新时间:2023-12-01 15:57:32 28 4
gpt4 key购买 nike

我有两个 Single 值,它们包含同一对象的数组。我想合并它们并应用 map 并将它们作为单个返回。可能吗?

所以我有这个:

func fetchTripList(type: TripType) -> Single<[Trip]> {
let response: Single<APITripListResponse> = request(target: TripsAPI.fetchUpcomingTripList)
return response.map { $0.toModel() }
}

我需要这样的东西:

func fetchTripList(type: TripType) -> Single<[Trip]> {
let responseUpcoming: Single<APITripListResponse> = request(target: TripsAPI.fetchUpcomingTripList)
let responsePast: Single<APITripListResponse> = request(target: TripsAPI.fetchPastTripList)
//'Declare a Singe variable <[Trip]> called X merge both responseUpcoming and responsePast, apply to them map { $0.toModel() } and return it'

return X
}

非常感谢您的帮助。

最佳答案

如果认为 merge 运算符正是您想要的,它将等到您从两个请求中获取数据,然后将它们合并到一个数组中,您可以使用 map 。之后,您可以将其转换为 single

看起来像这样:

Observable.merge(responseUpcoming.asObservable(), responsePast.asObservable()).map { $0.toModel() }.asSingle()

关于ios - 合并两个单一响应 RXSwift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58765403/

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