I am working on a use case where the first API call will fetch a list of metadata elements and based on the output, the next API call will parallelly fetch contents for each metadata. Finally, I need to collect all the output and return an array of contents ([Content]) by updating some values from metadata.
- If the first call fails, then it shouldn't proceed to the next call
- data fetching can fail in 2nd API call for any metadata and if all fails it has to return a failure/error.
struct MetaData: Codable {
var identifier: String?
var type: String?
var url: String?
}
struct Content: Codable {
var contentId: String? // this is from metaData
var type: String? // this is from metaData
var title: String?
var subTitle: String?
var subscriptionValue: String?
}
I am very new to Combine and still on the learning curve.
I created two methods
func fetchMetaData() -> AnyPublisher<[MetaData], Error> {
return Future<[MetaData], Error> { promise in
//network call
//if success {
//promise(.success([MetaData]))
//} else {
//promise(.failure(Error()))
//}
}.eraseToAnyPublisher()
}
func fetchContents(metaData: MetaData) -> AnyPublisher<Content, Error> {
return Future<Content, Error> { promise in
//network call
//if success {
//promise(.success(Content))
//} else {
//promise(.failure(Error()))
//}
}.eraseToAnyPublisher()
}
How can I nest these functions and get the updated contents with type and identifier from metadata and handle failure scenarios?