我有一个ContentService
要求发表文章。该文章响应包含一个authorId
属性。
我有一个ProfileService
,允许我通过userId
请求用户个人资料。
我正在尝试从ContentService
请求一篇文章,使用ProfileService
属性将请求链接到完成authorId
的请求上,然后我想返回一个{{ 1}},其中包含文章和个人资料信息。
我的ContentArticleViewModel
看起来像这样-
ArticleInteractor
很简单final class ArticleInteractor: ArticleInteractorInputProtocol {
let fetchArticleTrigger = PublishSubject<String>()
private lazy var disposeBag = DisposeBag()
weak var output: ArticleInteractorOutputProtocol? {
didSet {
configureSubscriptions()
}
}
private func configureSubscriptions() {
guard let output = output else { return }
fetchArticleTrigger
.bind(to: dependencies.contentSvc.fetchContentByIdTrigger)
.disposed(by: disposeBag)
dependencies.contentSvc.fetchContentByIdResponse
.bind(to: output.fetchArticleResponse)
.disposed(by: disposeBag)
}
}
开始一个请求,然后我在fetchArticleTrigger
上bind
并接收响应。
我的dependencies.contentSvc.fetchContentByIdResponse
上的方法是-
ContentService
我的 // MARK:- FetchContentById
// @params: id - String
// return: PublishSubject<ContentArticle>
fetchContentByIdTrigger
.flatMapLatest { [unowned self] in self.client.request(.getcontentById(id: $0)) }
.map { (resp: Result<ContentArticle>) in
guard case .success(let props) = resp else { return ContentArticle() }
return props
}
.bind(to: fetchContentByIdResponse)
.disposed(by: disposeBag)
上有一个非常相似的设置-
ProfileService
我想我将为该文章创建一个模型,例如-
// MARK:- FetchUserProfileById
// @params: id - String
// return: PublishSubject<User>
fetchUserProfileByIdTrigger
.flatMapLatest { [unowned self] in self.client.request(.getProfileByUserId(id: $0)) }
.map { (resp: Result<User>) in
guard case .success(let props) = resp else { return User() }
return props
}
.bind(to: fetchUserProfileByIdResponse)
.disposed(by: disposeBag)
我正在struct ContentArticleViewModel {
var post: ContentArticle
var user: User
}
-
ArticleInteractor
但是我完全迷失了如何最好地处理这个问题。我看过许多有关链接请求的文章,但都在努力成功地应用任何内容。
编辑
我目前有工作-
dependencies.contentSvc.fetchContentByIdResponse
.flatMapLatest { article in
/* fetch profile using `article.authorId */
}.map { article,profile in
return ContentArticleViewModel(post: article,user: profile)
}
.bind(to: output.fetchArticleResponse)
.disposed(by: disposeBag)
但是我不确定这是正确的。