The data that I need to create a model are split and only receivable via two API requests.
The first (query.snapshotPublisher()
) publisher will get the main part of the model (inclusive it's ID) and the second request (getPersonalLikeCount(blogModel: blogModel, userID: "408VYXScmxUtUQB91EoTf0LEMgj2")
) will get some additional data and needs the ID (which is part of the model that's why I'm passing the complete model) that I got from the first request.
If both requests would be independent I would use the .Zip(other: )
operator but since the second one depends on the first one I tried the following to be able to pass the model to the second publisher.
But with this approach the second publisher get's cancelled after sending the first value and therefore won't update the model even when the getPersonalLikeCount
publisher gets new data and should publish them again.
private func getBlogs(from query: Query) -> AnyPublisher<BlogModel, Error> {
let publisher = query.snapshotPublisher()
.flatMap { [self] blogModel -> AnyPublisher<(BlogModel, Int), Never> in
let blogModelPublisher = Just(blogModel)
let likeCountPublisher = getPersonalLikeCount(blogModel: blogModel, userID: "408VYXScmxUtUQB91EoTf0LEMgj2")
let combined = Combine.Publishers.Zip(blogModelPublisher, likeCountPublisher)
return combined.eraseToAnyPublisher()
}
.map { blogModel, personalLikeCount -> BlogModel in
var newModel = blogModel
newModel.personalLikeCount = personalLikeCount
return newModel
}
.eraseToAnyPublisher()
return publisher
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…