SafeExecutor is event-based error handler.
- error listener
- add event
- retry policy
- thread scheduler
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
orcarModel.detail
can be nullcarNameTextView
can be nullsetText
is may not be able to run on other thread- etc....
Of 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.
- If first
setText
fails, rests are not executed. - You can use
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}'