合并两个单个响应RXSwift

问题描述 投票:1回答:1

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

所以我有这个:

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
    }

非常感谢您的帮助。

ios swift rx-swift
1个回答
0
投票

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

看起来像这样:

Observable.merge(responseUpcoming, responsePast).map { $0.toModel() }.asSingle()
© www.soinside.com 2019 - 2024. All rights reserved.