Realm Change Listener with RealmResults not being called

孤人 提交于 2019-11-28 01:35:18

问题


I'm using Realm 3.0.0

I'm fetching some objects from Realm and trying to add onChangeListener, but it does not get fired when an object is changed

Here's the code, am I missing something here?

 RealmResults<Record> realmResults = RealmManager.recordsDao().loadRecords();
    realmResults.addChangeListener(new RealmChangeListener<RealmResults<Record>>() {
        @Override
        public void onChange(RealmResults<Record> element) {
            for (int i = 0; i < recordList.size(); i++) {
                if (collection.get(i).getId().equals(recordList.get(i).getId())) {
                    recordList.set(i, collection.get(i));
                    adapter.notifyItemChanged(i);
                }

            }
        }
    });

Also as per the docs, it mentions to call invalidateView(); but even that does not reflect the new data

The change to the object is made in the adapter

public class RecordsAdapter extends RecyclerView.Adapter<RecordsAdapter.ViewHolder> {

private ArrayList<Record> recordList;
private Context context;
private Realm realm = Realm.getDefaultInstance();

public RecordsAdapter(ArrayList<Record> recordList, Context context) {
    this.recordList = recordList;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_records, parent, false);
    return new ViewHolder(v);
}


@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.batsmanName.setText(recordList.get(position).getName());
    Glide.with(context).load(recordList.get(position).getImage()).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(holder.profilePicture);
    holder.totalRuns.setText("Runs " + recordList.get(position).getTotalScore());
    holder.totalMatches.setText("Matches " +recordList.get(position).getMatchesPlayed());


    if (recordList.get(position).isFavourite())
        holder.favCheck.setChecked(true);
    else
        holder.favCheck.setChecked(false);


    holder.favCheck.setOnClickListener(v -> {
        CheckBox checkBox = (CheckBox) v;
        if (checkBox.isChecked()) {
            realm.executeTransaction(realm1 -> {
                recordList.get(position).setFavourite(true);
                realm.copyToRealmOrUpdate(recordList);
            });
        } else {
            realm.executeTransaction(realm12 -> {
                recordList.get(position).setFavourite(false);
                realm.copyToRealmOrUpdate(recordList);
            });
        }

    });


}

回答1:


You need to store the RealmResults as a field reference in order to ensure that Realm can update it

RealmResults<Record> realmResults;

public void etc() {
    realmResults = RealmManager.recordsDao().loadRecords();
    realmResults.addChangeListener(new RealmChangeListener<RealmResults<Record>>() {

Also, you should probably use RealmRecyclerViewAdapter from https://github.com/realm/realm-android-adapters with RealmResults<Record>, instead of ArrayList so that you actually keep a managed results in sync with your recycler view automatically


So with that in mind, all you need to do is replace your code with

public class RecordsAdapter extends RealmRecyclerViweAdapter<Record, RecordsAdapter.ViewHolder> {
     public RecordsAdapter(OrderedRealmCollection<Record> realmResults) {
         super(realmResults, true);
     }

     // ... same as before
}

and

recyclerView.setAdapter(new RecordsAdapter(RealmManager.recordsDao().loadRecords());

And just ditch your RealmChangeListener because it is incomplete and unnecessary



来源:https://stackoverflow.com/questions/42761687/realm-change-listener-with-realmresults-not-being-called

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!