【发布时间】:2020-04-29 15:55:59
【问题描述】:
我有一个问题,我不知道如何用更好的方法解决它。问题是我正在向 Spotify Web API 请求,并且在某些方法中返回了艺术家图像,而在其他方法中只获得了基本的艺术家信息。
我有这两种方法:
fun getAlbum(albumId: String): Single<SpotifyAlbumDTO>
fun getArtist(artistId: String): Single<SpotifyArtistDTO>
当我获得专辑时,艺术家信息不包含艺术家图片网址。因此,我需要调用 getAlbum() 方法并使用结果获取 artistId,然后调用 getArtist() 方法。
我有以下方法来做所有这些事情:
fun getAlbum(albumId: String): Single<Album>
在这个方法上,我需要调用前两个来返回一个专辑对象(我的域对象)。唯一对我有用的解决方案如下:
fun getAlbum(albumId: String): Single<Album> {
return Single.create { emitter ->
_spotifyService.getAlbum(albumId).subscribe { spotifyAlbum ->
_spotifyService.getArtist(spotifyAlbum.artist.id).subscribe { spotifyArtist ->
val artistImage = spotifyArtist.imageUrl
spotifyAlbum.artist.image = artistImage
emitter.onNext(spotifyAlbum.toAlbum())
}
}
}
}
我认为必须存在另一种更好的方法来做到这一点,而不是在其他订阅中连接订阅调用并创建更深入的调用。我也尝试以下方法:
_spotifyService.getAlbum(albumId).flatMap { spotifyAlbum ->
_spotifyService.getArtist(spotifyAlbum.artist.id)
}.flatMap { spotifyArtist ->
// Here I don't have the album and I can't to asign the image
}
【问题讨论】:
标签: rx-java system.reactive rx-kotlin