question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RealmChangeListener.onChange not call after change in Realm

See original GitHub issue

Realm 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:closed
  • Created 6 years ago
  • Comments:28 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
Zhuindencommented, Oct 31, 2017

Yeah although you can also try it and see if it works

1reaction
beeendercommented, Oct 31, 2017
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found