简体   繁体   English

使用 SRP 时在 Jetpack Compose 中处理网络响应的最佳方式

[英]Best way to handle network response in jetpack compose when using SRP

When using the Single Responsibility pattern, I wonder what would be the best approach to show.network response (Success, error, progress).使用单一职责模式时,我想知道显示网络响应(成功、错误、进度)的最佳方法是什么。 storing state for every request would create so many states in viewModel and will have to pass so many states to a component.为每个请求存储 state 会在 viewModel 中创建如此多的状态,并且必须将如此多的状态传递给组件。 Any other way, worth a try?还有什么办法值得一试吗?

I use this in my main Activity's onCreate function to capture all unhandled exceptions but only when the app is released for production.我在主 Activity 的onCreate function 中使用它来捕获所有未处理的异常,但仅当应用程序发布用于生产时。 During debug, I want the app to crash so that the exception stack is printed to LogCat:在调试期间,我希望应用程序崩溃,以便将异常堆栈打印到 LogCat:

if (!BuildConfig.DEBUG) {
    Thread.setDefaultUncaughtExceptionHandler { paramThread, paramThrowable ->
         val message: String

         if (paramThrowable is HttpException) {
              message = R.string.problem_processing_request.stringRes()
         } else {
              message = R.string.unknown_problem.stringRes()
         }

         App.mainViewModel.displaySnackbar(sbInfo = SnackbarInfo(message = message, isCritical = true))
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM