I'm developing Android app which should use pagination on RecyclerView
. I use Executor
within Service
to make webrequests to API and then persist fetched data in DB. To notify about changes in DB and then apply new data in my adapter I suggest to use Otto event bus. Publisher/subscriber pattern is quite new for me, so I tried to find good tutorial or explanation how this should work but after two days of search I have only foggy idea about what should I implement and how actually Event bus works. Could someone give a good hint where should I start or link on tutorial or code sample? i know that this is a really newbie question but I don't see alternative for this moment.
Asked
Active
Viewed 836 times
2

Autumn_Cat
- 790
- 1
- 14
- 28
2 Answers
1
The best guides that I found are: from Vogella and from Codepath. I hope it will help somebody. And good note based on my experience - Otto EventBus works synchronously , keep it in mind.

Autumn_Cat
- 790
- 1
- 14
- 28
-1
import this library into your app level gradel:
implementation 'org.greenrobot:eventbus:3.0.0'
create a class for Event handling:
public class DataSyncEvent {
private final String syncStatusMessage;
private final String countryName;
private final int postion;
public DataSyncEvent(String syncStatusMessage, int postion, String countryName) {
this.syncStatusMessage = syncStatusMessage;
this.postion = postion;
this.countryName = countryName;
}
public String getSyncStatusMessage() {
return syncStatusMessage;
}
public String countryName() {
return countryName;
}
public int getPostion() {
return postion;
}
}
now pass values from your class/fragment or service:
EventBus.getDefault().post(new DataSyncEvent(leftOrRight, position, countryName));
don't forget to register and unregister bus where you are using @Subscribe
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
with @Subscribe you can get that values:
@Subscribe
public void onEvent(DataSyncEvent syncStatusMessage) {
if (syncStatusMessage.getSyncStatusMessage().contains("left")) {
leftPosition = syncStatusMessage.getPostion();
img_flag_left.setImageResource(countriesFlag[leftPosition]);
leftCountryName.setText(syncStatusMessage.countryName());
} else {
rightPosition = syncStatusMessage.getPostion();
img_flag_right.setImageResource(countriesFlag[rightPosition]);
rightCountryName.setText(syncStatusMessage.countryName());
}
}

Null Pointer Exception
- 1,555
- 14
- 13