无法从 ViewModel 向用户显示协程错误消息
Cannot display coroutine error message to users from ViewModel
我正在尝试在 Retrofit API 调用出现问题时向用户显示错误消息。我正在使用 Kotlin Coroutines、Kodein 和 MVVM 模式。鉴于在片段中并未真正观察到 MutableLiveData exceptionMessage,我无法在 toast 消息中显示错误消息(我猜这与通过 [=20= 获取数据的异步函数的性质有关) ] (暂停乐趣)).
视图模型:
class BarcodeViewModel(
private val barcodeRepository: BarcodeRepository,
private val productRepository: ProductRepository
) : ViewModel() {
var exceptionMessage: MutableLiveData<String> = MutableLiveData()
private val handler = CoroutineExceptionHandler { _, exception ->
exceptionMessage.value = exception.localizedMessage
}
fun getBarcodeData(barcode: String) {
CoroutineScope(Dispatchers.Main).launch(handler) {
val currentArticle = barcodeRepository.getProductData(barcode)
for (article in currentArticle.products) {
val articleToAdd =
Product(...)
val articleDb = productRepository.getProduct(barcode)
if (articleDb.isEmpty()) {
productRepository.addProduct(articleToAdd)
exceptionMessage.value = ""
} else {
exceptionMessage.value = "Product already exists"
}
}
}
}
}
片段:
class ArticleAddFragment : Fragment(), LifecycleOwner, KodeinAware {
override val kodein: Kodein by kodein()
private val factory: BarcodeViewModelFactory by instance()
private lateinit var viewModel: BarcodeViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_article_add, container, false)
viewModel = ViewModelProviders.of(this, factory).get(BarcodeViewModel::class.java)
...
return view
}
private fun processResult(firebaseVisionBarcodes: List<FirebaseVisionBarcode>) {
if (firebaseVisionBarcodes.isNotEmpty()) {
for (item in firebaseVisionBarcodes) {
when (item.valueType) {
FirebaseVisionBarcode.TYPE_PRODUCT -> {
viewModel.getBarcodeData(item.rawValue!!)
viewModel.exceptionMessage.observe(viewLifecycleOwner, Observer {
it?.let {
if (!it.isBlank()) {
Toast.makeText(context, it, Toast.LENGTH_SHORT).show()
}
}
})
...
}
}
}
}
}
在 toast 中显示错误消息的最聪明的方法是什么?
我建议使用 Sealed 类 或 Enum 来更新和观察状态。
State.kt
sealed class State<T> {
class Loading<T> : State<T>()
data class Success<T>(val data: T) : State<T>()
data class Error<T>(val message: String) : State<T>()
companion object {
/**
* Returns [State.Loading] instance.
*/
fun <T> loading() = Loading<T>()
/**
* Returns [State.Success] instance.
* @param data Data to emit with status.
*/
fun <T> success(data: T) = Success(data)
/**
* Returns [State.Error] instance.
* @param message Description of failure.
*/
fun <T> error(message: String) = Error<T>(message)
}
}
类是通用的,所以大家可以根据成功和失败来相应的放数据。
我正在尝试在 Retrofit API 调用出现问题时向用户显示错误消息。我正在使用 Kotlin Coroutines、Kodein 和 MVVM 模式。鉴于在片段中并未真正观察到 MutableLiveData exceptionMessage,我无法在 toast 消息中显示错误消息(我猜这与通过 [=20= 获取数据的异步函数的性质有关) ] (暂停乐趣)).
视图模型:
class BarcodeViewModel(
private val barcodeRepository: BarcodeRepository,
private val productRepository: ProductRepository
) : ViewModel() {
var exceptionMessage: MutableLiveData<String> = MutableLiveData()
private val handler = CoroutineExceptionHandler { _, exception ->
exceptionMessage.value = exception.localizedMessage
}
fun getBarcodeData(barcode: String) {
CoroutineScope(Dispatchers.Main).launch(handler) {
val currentArticle = barcodeRepository.getProductData(barcode)
for (article in currentArticle.products) {
val articleToAdd =
Product(...)
val articleDb = productRepository.getProduct(barcode)
if (articleDb.isEmpty()) {
productRepository.addProduct(articleToAdd)
exceptionMessage.value = ""
} else {
exceptionMessage.value = "Product already exists"
}
}
}
}
}
片段:
class ArticleAddFragment : Fragment(), LifecycleOwner, KodeinAware {
override val kodein: Kodein by kodein()
private val factory: BarcodeViewModelFactory by instance()
private lateinit var viewModel: BarcodeViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_article_add, container, false)
viewModel = ViewModelProviders.of(this, factory).get(BarcodeViewModel::class.java)
...
return view
}
private fun processResult(firebaseVisionBarcodes: List<FirebaseVisionBarcode>) {
if (firebaseVisionBarcodes.isNotEmpty()) {
for (item in firebaseVisionBarcodes) {
when (item.valueType) {
FirebaseVisionBarcode.TYPE_PRODUCT -> {
viewModel.getBarcodeData(item.rawValue!!)
viewModel.exceptionMessage.observe(viewLifecycleOwner, Observer {
it?.let {
if (!it.isBlank()) {
Toast.makeText(context, it, Toast.LENGTH_SHORT).show()
}
}
})
...
}
}
}
}
}
在 toast 中显示错误消息的最聪明的方法是什么?
我建议使用 Sealed 类 或 Enum 来更新和观察状态。
State.kt
sealed class State<T> {
class Loading<T> : State<T>()
data class Success<T>(val data: T) : State<T>()
data class Error<T>(val message: String) : State<T>()
companion object {
/**
* Returns [State.Loading] instance.
*/
fun <T> loading() = Loading<T>()
/**
* Returns [State.Success] instance.
* @param data Data to emit with status.
*/
fun <T> success(data: T) = Success(data)
/**
* Returns [State.Error] instance.
* @param message Description of failure.
*/
fun <T> error(message: String) = Error<T>(message)
}
}
类是通用的,所以大家可以根据成功和失败来相应的放数据。