未调用RealmResults的领域更改侦听器 [英] Realm Change Listener with RealmResults not being called

查看:89
本文介绍了未调用RealmResults的领域更改侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Realm 3.0.0

I'm using Realm 3.0.0

我正在从Realm中获取一些对象,并尝试添加onChangeListener,但是当对象更改时,它不会被触发

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);
                }

            }
        }
    });

此外,根据文档,它提到要调用invalidateView(); ,但即使这样也不能反映新数据

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);
            });
        }

    });


}

推荐答案

您需要将RealmResults存储为字段引用,以确保Realm可以对其进行更新

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>>() {

此外,您可能应该使用 https://github.com/realm/中的RealmRecyclerViewAdapter Realm-android-adapters RealmResults<Record>而不是ArrayList,这样您实际上可以使托管结果自动与回收者视图保持同步

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
}

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

然后抛弃您的RealmChangeListener,因为它不完整且不必要

And just ditch your RealmChangeListener because it is incomplete and unnecessary

这篇关于未调用RealmResults的领域更改侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆