java 如何将 Observable.fromCallable() 与已检查的异常一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36208331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:04:21  来源:igfitidea点击:

How to use Observable.fromCallable() with a checked Exception?

javaandroidrx-javareactive-programming

提问by ashughes

Observable.fromCallable()is great for converting a single function into an Observable. But how do you handle checked exceptions that might be thrown by the function?

Observable.fromCallable()非常适合将单个函数转换为 Observable。但是如何处理函数可能抛出的已检查异常呢?

Most of the examples I've seen use lambdas and "just work". But how would you do this without lambdas? For example, see the quote below from this great article:

我见过的大多数示例都使用 lambdas 并且“正常工作”。但是,如果没有 lambda,您将如何做到这一点?例如,请参阅下面这篇精彩文章中的引用:

Observable.fromCallable(() -> downloadFileFromNetwork());

It's a one-liner now! It deals with checked exceptions, no more weird Observable.just() and Observable.error() for such easy thing as deferring code execution!

现在是单线!它处理已检查的异常,不再是奇怪的 Observable.just() 和 Observable.error() 延迟代码执行之类的简单事情!

When I attempt to implement the above Observable without a lambda expression, based on other examples I've seen, and how Android Studio auto-completes, I get the following:

当我尝试在没有 lambda 表达式的情况下实现上述 Observable 时,根据我看到的其他示例以及 Android Studio 自动完成的方式,我得到以下信息:

Observable.fromCallable(new Func0<File>() {
    @Override
    public File call() {
        return downloadFileFromNetwork();
    }
}

But if downloadFileFromNetwork()throws a checked exception, I have to try-catch it and wrap it in a RuntimeException. There's got to be a better way! How does the above lambda support this?!?!

但是如果downloadFileFromNetwork()抛出一个已检查的异常,我必须尝试捕获它并将其包装在RuntimeException. 必须有更好的方法!上面的 lambda 如何支持这个?!?!

回答by ashughes

Rather than using a Func0with Observable.fromCallable(), use Callable. For example:

与其使用Func0with Observable.fromCallable(),不如使用Callable. 例如:

Observable.fromCallable(new Callable<File>() {
    @Override
    public File call() throws Exception {
        return downloadFileFromNetwork();
    }
}

Since Callable's method call()throws Exception, you don't have to wrap your function in a try-catch! This must be what the lambda is using under the hood.

由于Callable的方法call()throws Exception,您不必将函数包装在 try-catch 中!这一定是 lambda 在幕后使用的东西。

回答by cesards

You could also do this to return checked exceptions:

您也可以这样做以返回已检查的异常:

return Observable.fromCallable(() -> {
  sharedPreferences.edit()
          .putString(DB_COMPANY, LoganSquare.serialize(
                  CompanyDBTransformation.get(user.getCompany())
          ))
          .apply();
  return user;
  }).onErrorResumeNext(
            throwable -> Observable.error(new CompanySerializationException(throwable))
    );

So here I'm serializing taking the IOException risk, and I'm giving back a more descriptive description.

所以在这里,我正在对 IOException 风险进行序列化,并给出更具描述性的描述。