RealmChangeListener.onChange not call after change in Realm
See original GitHub issueRealm 4.1.
My fragment code:
public class FragmentSort extends Fragment {
private Realm realm;
private RealmResults<Offer> offersRealmResults;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.offers_sort_fragment, container, false);
init(rootView);
return rootView;
}
private void init(View rootView) {
ButterKnife.bind(this, rootView);
realm = Realm.getDefaultInstance();
offersRealmResults = StoreService.getOffers(realm,orderBy);
initLogic()
}
private void initLogic() {
offersRealmResults.addChangeListener(new RealmChangeListener<RealmResults<Offer>>() {
@Override
public void onChange(RealmResults<Offer> offers) {
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
recyclerView.setAdapter(null);
realm.removeAllChangeListeners();
realm.close();
}
}
Here StoreService’s method:
public static RealmResults<Offer> getOffers(Realm realm, String sortByFieldName) {
return realm.where(Offer.class).findAllSorted(sortByFieldName, Sort.DESCENDING);
}
Here my adapter code:
public class OfferSortAdapter extends RealmRecyclerViewAdapter<Offer, OfferSortAdapter.OfferViewHolder> implements RecyclerOnItemClickListener {
public void onItemClicked(View view, int position) {
Realm realm = Realm.getDefaultInstance();
try {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Offer offer = realm.where(Offer.class).equalTo(Offer.ID, offerId).findFirst();
offer.setDownloaded(downloaded);
}
});
} finally {
realm.close();
}
}
}
After call method onItemClicked
the offer success changed in Realm. Changed field “downloaded” from from false to true.
But method onChange()
in Fragment not call.
Why?
Issue Analytics
- State:
- Created 6 years ago
- Comments:28 (15 by maintainers)
Top Results From Across the Web
RealmResults not calls RealmChangeListener.onChange()
but listener's method onChange() not calls when data had been changed. Listener works only when added to realm. Realm.getDefaultInstance().
Read more >Realm: Create reactive mobile apps in a fraction of the time
When the background thread adds new customers, the data in the UI thread will be automatically updated. The UI thread gets notified via...
Read more >Create reactive mobile apps in a fraction of the time - Realm
public class Person extends RealmObject { @Required // Name is not ... void onChange() { // This is called anytime the Realm database...
Read more >Creating a Reactive Data Layer with Realm and RxJava2
RealmChangeListener realmChangeListener = new ... Since Realm 3.1, these so-called coarse grained change listeners became fine-grained ...
Read more >Java Examples for io.realm.RealmChangeListener - Javatips.net
This java examples will help you to understand the usage of io.realm. ... "onChange() called with: puppies.size(); = [" + puppies.size() + "]");...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yeah although you can also try it and see if it works
RealmRecyclerViewAdapter
should handle refreshing UI automatically. See examples https://github.com/realm/realm-android-adapters/blob/master/example/src/main/java/io/realm/examples/adapters/ui/recyclerview/MyRecyclerViewAdapter.java