Если вы изменяете RealmObject в фоновом потоке, как правило, правильным способом обработки этого обновления является наличие RealmChangeListener поверх RealmResults, который будет содержать этот RealmObject.
Обрабатывать любое изменение фонового потока через RealmChangeListener, который наблюдает за изменениями.
private Realm realm;
private RealmResults<UserTripModel> trips;
private RealmChangeListener<RealmResults<UserTripModel>> realmChangeListener = trips -> {
// Set the trips to the adapter only when async query is loaded.
// It will also be called for any future writes made to the Realm.
// note that this is also where you could call `setMessageCount`.
adapter.setData(trips);
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_realm_example);
// This is the RecyclerView adapter
adapter = new TripsAdapter();
//This is the RecyclerView which will display the list of cities
recyclerView = findViewById(R.id.cities_list);
recyclerView.setAdapter(adapter);
recyclerView.setOnItemClickListener(RecyclerViewExampleActivity.this);
// Create a new instance of Realm
realm = Realm.getDefaultInstance();
// Obtain the cities in the Realm with asynchronous query.
trips = realm.where(UserTripModel.class).findAllAsync();
// The RealmChangeListener will be called when the results are asynchronously loaded, and available for use.
trips.addChangeListener(realmChangeListener);
}
@Override
protected void onDestroy() {
super.onDestroy();
trips.removeAllChangeListeners(); // Remove change listeners to prevent updating views not yet GCed.
realm.close(); // Remember to close Realm when done.
}