【发布时间】:2020-03-20 09:06:59
【问题描述】:
我查询了 youtube api 并制作了几个模型来使用 gson 转换器映射 json。现在我想使用存储库模式并统一来自我的数据库和远程的返回值,但是我有一些问题要知道如何从 RxJava 中的可观察对象返回特定变量。
我这样查询api:
@GET("playlistItems?part=snippet")
fun getLatestVideosFromPlaylist(@Query("playlistId") playlistId:String, @Query("maxResults") maxResults:String): Observable<YoutubeVideosModel>
我的模型
data class YoutubeVideosModel(
@SerializedName("items")
val videos: ArrayList<YoutubeVideo>
)
@Entity(tableName = "video")
data class YoutubeVideo(
@PrimaryKey(autoGenerate = true)
val id: Int? = null,
@SerializedName("snippet")
val video: YoutubeVideoData
)
我的数据源界面
interface VideoDataSource {
fun fetchVideos(playlistId:String) : Observable<ArrayList<YoutubeVideo>>
}
从我的本地数据源返回一个 youtubeVideo 的数组列表
object VideoLocalSource: VideoDataSource {
override fun fetchVideos(playlistId: String): Observable<ArrayList<YoutubeVideo>> {
return Observable.fromCallable {
AppDatabase.getInstance(BaseApp.INSTANCE)?.getVideoDao()!!.fetchAllVideos()
}
}
}
但是从我的遥控器我找不到如何返回相同的内容:
object VideoRemoteSource: VideoDataSource {
override fun fetchVideos(playlistId: String, maxResults:String): Observable<ArrayList<YoutubeVideo>> {
YoutubeApiClient.getYoutubeService().getLatestVideosFromPlaylist(playlistId, maxResults)
.subscribe {
videoModel ->
//Here cannot use return
return ObservableFromArray(arrayOf(videoModel.videos)
}
}
}
【问题讨论】:
标签: android asynchronous kotlin rx-java