android - 调用 viewModelScope 仅在第一次调用时执行
问题描述
我有一个执行 api 调用的 ViewModel 方法。由于某种原因,我只能执行一次。
class MyViewModel : ViewModel() {
fun apiCall() {
// Function call gets here every time.
// viewModelScope is not active on subsequent calls
viewModelScope.launch {
// Only gets inside here the first call
executeApiCall()
}
}
suspend fun executeApiCall() = withContext(Dispatchers.Default) {
// Only gets inider here the first call
}
它每次都清楚地进入“apiCall()”函数,但无法使用 viewModelScope 执行作业。
我认为这与我的 viewModelScope 对后续调用不活跃有关,但我不确定我必须做什么才能使其再次活跃。作为冰雹结婚,也尝试使用不同的调度程序。
解决方案
推荐阅读
- mysql - 计算百分比两个值和求和算术运算mysql
- swift - 无法在 RxSwift 中使用通用结果枚举错误
- reactjs - React Native - TypeError:this.addItem.bind
- php - 如何制作具有多个选项卡的过滤器并在laravel中动态获取数据
- android - 在 AsyncTask 中使用 onPostExecute 方法列表项单击事件调用 Android 中的另一个活动
- c++ - 如何在 C++ 中按值对地图进行排序,但如果键值相等?
- javascript - jQuery - onClick 在新页面中打开随机链接
- javascript - 通过 Jenkins 启动 Eclipse,变量从 jenkins 传递到 eclipse
- ios - 如何通过 CLI 擦除 Xcode 10 模拟器克隆?
- php - 带有阿拉伯字母的图像名称未显示在 html 页面中