java fail safe executor
SafeExecutor is event-based error handler.
Before you know SafeExecutor,
private void bindData(CarModel carModel) {
carNameTextView.setText(carModel.detail.carName);
carPriceTextView.setText(carModel.detail.price.toString());
carNumberTextView.setText(carModel.detail.number.toString());
}
There are many problems.
carModel
or carModel.detail
can be nullcarNameTextView
can be nullsetText
is may not be able to run on other threadOf course, you can use try-catch
like below.
private void bindData(CarModel carModel) {
try {
carNameTextView.setText(carModel.detail.carName);
carPriceTextView.setText(carModel.detail.price.toString());
carNumberTextView.setText(carModel.detail.number.toString());
}
catch(Exception exception){
logException(exception);
}
}
But there are still some problems.
setText
fails, rests are not executed.try-catch
on every line, but it’s uncomfortable.After you know SafeExecutor,
private void bindData(CarModel carModel) {
SafeExecutor.build()
.add(() -> carNameTextView.setText(carModel.detail.carName))
.add(() -> carPriceTextView.setText(carModel.detail.price.toString()))
.add(() -> carNumberTextView.setText(carModel.detail.number.toString()))
.executeOn(Scheduler.MainThread)
.onError(throwable -> logException(throwable))
.run();
}
Simple and safe and… looks cool!!
Maven
<dependency>
<groupId>net.jspiner</groupId>
<artifactId>safeexecutor</artifactId>
<version>{VERSION_CODE_HERE}</version>
<type>pom</type>
</dependency>
Gradle
compile 'net.jspiner:safeexecutor:{VERSION_CODE_HERE}'