Android Kotlin 协程崩溃,在 Logcat 中没有报告致命错误



我的代码中反复发生崩溃,似乎无缘无故地开始发生。 我的代码编译正确,然后应用程序在调用视图模型后立即崩溃,并且不会在日志中报告错误!!这是我第一次看到没有在日志中报告错误的崩溃。通过使用 Timber 语句,我能够弄清楚代码崩溃是在实时数据更新之前在 withContext 行上。我的问题是:我如何查看致命错误,以便知道如何解决崩溃?

class DashboardViewModel : ViewModel() {
private var delDate = ""
private var thread: Job = Job()
private var duration: Int = ConfigData.refresh * 1000
private var counter: Int = 0
private var area: String = "All"
var estFinishLiveData: MutableLiveData<String> = MutableLiveData()
var trailingAreaLiveData: MutableLiveData<AreaModel> = MutableLiveData()
var percentCompleteLiveData: MutableLiveData<OverallModel> = MutableLiveData()
var chartDataLiveData: MutableLiveData<ArrayList<ChartModel>> = MutableLiveData()
fun loadDataRefresher(periodic: Boolean, selectedArea: String) {
counter++
area = if (selectedArea == "All") "" else selectedArea
Timber.d("Dashboard update $counter for $selectedArea next in ${ConfigData.refresh} seconds")
thread = CoroutineScope(Dispatchers.IO).launch {
loadDelDate()
loadEstimatedFinish()
loadTrailingArea()
loadPercentComplete()
loadChartData()
if (periodic) {
delay(duration.toLong())
loadDataRefresher(false, selectedArea)
}
}
}
private fun loadDelDate() {
delDate = DatabaseMgr.spRetrieveDeliveryDate("")
}
private suspend fun loadEstimatedFinish() {
val estFinish = DatabaseMgr.spRetrieveEstimatedFinish(delDate)
withContext(Dispatchers.Main) {
estFinishLiveData.value = estFinish
}
}
private suspend fun loadTrailingArea() {
val areaModel = DatabaseMgr.spRetrieveTrailingArea(delDate)
if (areaModel.description == "") {
areaModel.description = "None in progress"
}
withContext(Dispatchers.Main) {
trailingAreaLiveData.value = areaModel
}
}

我找到了自己的解决方案。 显然,更新的 Android jetpack 核心版本与我从 jetbrain 使用的协程库之间存在冲突。 一旦我从 jetbrain 中删除了库,协程就完美地运行了。

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
// Testing
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//    // Coroutines
//    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0"  <- Causes a clash with jetpack's core libary
//    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"
// Dagger 2
implementation "com.google.dagger:dagger:2.24"
implementation "com.google.dagger:dagger-android:2.24"
kapt "com.google.dagger:dagger-compiler:2.24"
kapt "com.google.dagger:dagger-android-processor:2.24"
// View Model
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0'
}

相关内容

  • 没有找到相关文章

最新更新