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

前端 未结 2 881
面向向阳花
面向向阳花 2021-02-08 06:07

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 functio

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-08 06:32

    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.

提交回复
热议问题