有没有办法将 return 添加到协程?
Is there a way to add return to a coroutine?
我只想知道我是否可以 return activePodcastViewData。我得到 return not allow here anytime I try to call it on the activePodcastViewData.Without GlobalScope 我确实让一切正常 fine.However 我通过向 it.Hence 添加挂起方法更新了我的存储库我正在获取挂起函数只能从协程或另一个挂起函数中调用。
fun getPodcast(podcastSummaryViewData: PodcastViewModel.PodcastSummaryViewData): PodcastViewData? {
val repo = podcastRepo ?: return 空
val url = podcastSummaryViewData.url ?: return null
GlobalScope.launch {
val podcast = repo.getPodcast(url)
withContext(Dispatchers.Main) {
podcast?.let {
it.feedTitle = podcastViewData.name ?: ""
it.imageUrl = podcastViewData.imageUrl ?: ""
activePodcastViewData = PodcastView(it)
activePodcastViewData
}
}
}
return null
}
class PodcastRepo {
val rssFeedService =RssFeedService.instance
suspend fun getPodcast(url:String):Podcast?{
rssFeedService.getFeed(url)
return Podcast(url,"No name","No Desc","No image")
}
我不确定我是否理解正确,但如果您想从协程范围获取 activePodcastViewData,您应该使用一些可观察的数据容器。我将用 LiveData.
向您展示一个简单的示例
首先,添加实现:
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.0"
现在,在您的 ViewModel 中,我们需要创建 mutableLiveData 来保存和发出我们未来的数据。
val podcastsLiveData by lazy { MutableLiveData<Podcast>() }
这是你的方法:(我不推荐 GlobalScope,让我们替换它)
fun getPodcast(podcastSummaryViewData: PodcastViewModel.PodcastSummaryViewData): PodcastViewData? {
val repo = podcastRepo ?: return null
val url = podcastSummaryViewData.url ?: return null
CoroutineScope(Dispatchers.IO).launch {
val podcast = repo.getPodcast(url)
withContext(Dispatchers.Main) {
podcast?.let {
it.feedTitle = podcastViewData.name ?: ""
it.imageUrl = podcastViewData.imageUrl ?: ""
activePodcastViewData = PodcastView(it)
}
}
}
podcastsLiveData.postValue(activePodcastViewData)
}
如您所见,您的 return null 已转换为 postValue()。现在你终于可以从你的 Activity:
观察到这一点了
viewModel.podcastsLiveData.observe(this) {
val podcast = it
//Use your data
}
viewModel.getPodcast()
现在每次调用viewModel.getPodcast()方法时,都会调用observe中的代码。
我希望我能帮到一些人:D
我只想知道我是否可以 return activePodcastViewData。我得到 return not allow here anytime I try to call it on the activePodcastViewData.Without GlobalScope 我确实让一切正常 fine.However 我通过向 it.Hence 添加挂起方法更新了我的存储库我正在获取挂起函数只能从协程或另一个挂起函数中调用。
fun getPodcast(podcastSummaryViewData: PodcastViewModel.PodcastSummaryViewData): PodcastViewData? { val repo = podcastRepo ?: return 空 val url = podcastSummaryViewData.url ?: return null
GlobalScope.launch {
val podcast = repo.getPodcast(url)
withContext(Dispatchers.Main) {
podcast?.let {
it.feedTitle = podcastViewData.name ?: ""
it.imageUrl = podcastViewData.imageUrl ?: ""
activePodcastViewData = PodcastView(it)
activePodcastViewData
}
}
}
return null
}
class PodcastRepo { val rssFeedService =RssFeedService.instance
suspend fun getPodcast(url:String):Podcast?{
rssFeedService.getFeed(url)
return Podcast(url,"No name","No Desc","No image")
}
我不确定我是否理解正确,但如果您想从协程范围获取 activePodcastViewData,您应该使用一些可观察的数据容器。我将用 LiveData.
向您展示一个简单的示例首先,添加实现:
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.0"
现在,在您的 ViewModel 中,我们需要创建 mutableLiveData 来保存和发出我们未来的数据。
val podcastsLiveData by lazy { MutableLiveData<Podcast>() }
这是你的方法:(我不推荐 GlobalScope,让我们替换它)
fun getPodcast(podcastSummaryViewData: PodcastViewModel.PodcastSummaryViewData): PodcastViewData? {
val repo = podcastRepo ?: return null
val url = podcastSummaryViewData.url ?: return null
CoroutineScope(Dispatchers.IO).launch {
val podcast = repo.getPodcast(url)
withContext(Dispatchers.Main) {
podcast?.let {
it.feedTitle = podcastViewData.name ?: ""
it.imageUrl = podcastViewData.imageUrl ?: ""
activePodcastViewData = PodcastView(it)
}
}
}
podcastsLiveData.postValue(activePodcastViewData)
}
如您所见,您的 return null 已转换为 postValue()。现在你终于可以从你的 Activity:
观察到这一点了viewModel.podcastsLiveData.observe(this) {
val podcast = it
//Use your data
}
viewModel.getPodcast()
现在每次调用viewModel.getPodcast()方法时,都会调用observe中的代码。
我希望我能帮到一些人:D