组合执行嵌套请求
Combine perform nested requests
我是Combine的新手,请帮忙。我需要组合 2 URL 个请求来接收完整的数据。第一个带有 id,第二个请求应该在他们的请求中使用这些 id。我需要它在一个出版商。
所以我先表演:
func getDataFromServer(request: Request) -> AnyPublisher<MyData, NetworkError> {
return networking.xml(request)
.mapError { NetworkError.invalidData) }
.flatMap { response -> AnyPublisher< MyData, NetworkError> in
return Future< MyData, NetworkError>.init { promise in
guard let data = MyData.from(response: response) else {
return promise(.failure(NetworkError.parseError))
}
if data.myID.count > 0 {
**// Perform second request with data.myID as parameter
// receive and fill additional fields data in 'data' object**
}
promise(.success(MyData))
}.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
}
删除 Future 只需提出其他请求 return AnyPublisher< MyData, NetworkError>
或根据您的响应类型更改该类型
func getDataFromServer(request: Request) -> AnyPublisher<MyData, NetworkError> {
return networking.xml(request)
.mapError { NetworkError.invalidData) }
.flatMap { response -> AnyPublisher< MyData, NetworkError> in
guard let data = MyData.from(response: response) else {
return Fail(error: NetworkError.parseError).eraseToAnyPublisher()
}
if data.myID.count > 0 {
// Perform second request with data.myID as parameter
// receive and fill additional fields data in 'data' object
return networking.makeYourRequest(with id: data.myID) // makeYourRequest method shoul return publisher with same type or possible to conver or change flatMap return type
} else {
return Just(data).eraseToAnyPublisher()
}
}
.eraseToAnyPublisher()
}
我是Combine的新手,请帮忙。我需要组合 2 URL 个请求来接收完整的数据。第一个带有 id,第二个请求应该在他们的请求中使用这些 id。我需要它在一个出版商。 所以我先表演:
func getDataFromServer(request: Request) -> AnyPublisher<MyData, NetworkError> {
return networking.xml(request)
.mapError { NetworkError.invalidData) }
.flatMap { response -> AnyPublisher< MyData, NetworkError> in
return Future< MyData, NetworkError>.init { promise in
guard let data = MyData.from(response: response) else {
return promise(.failure(NetworkError.parseError))
}
if data.myID.count > 0 {
**// Perform second request with data.myID as parameter
// receive and fill additional fields data in 'data' object**
}
promise(.success(MyData))
}.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
}
删除 Future 只需提出其他请求 return AnyPublisher< MyData, NetworkError>
或根据您的响应类型更改该类型
func getDataFromServer(request: Request) -> AnyPublisher<MyData, NetworkError> {
return networking.xml(request)
.mapError { NetworkError.invalidData) }
.flatMap { response -> AnyPublisher< MyData, NetworkError> in
guard let data = MyData.from(response: response) else {
return Fail(error: NetworkError.parseError).eraseToAnyPublisher()
}
if data.myID.count > 0 {
// Perform second request with data.myID as parameter
// receive and fill additional fields data in 'data' object
return networking.makeYourRequest(with id: data.myID) // makeYourRequest method shoul return publisher with same type or possible to conver or change flatMap return type
} else {
return Just(data).eraseToAnyPublisher()
}
}
.eraseToAnyPublisher()
}