mardi 5 juillet 2016

Calling listview notifydatasetchanged() in Realmchangelistener is good to do?

We are using RealChangeListener to listen data changes and updating listview by calling notifydatasetgchanged().

Initial sync time we do get many records from server(per batch 100 records), loop thru results updating Realm like below in background thread.

for(int i=0;i<results.size();i++)
{
  // processing and validation
  ....
  db.beginTransaction();
  db.copyToRealm(processedObject);
  db.commitTransaction();
}

In activity, we are registered realmResults change listener like below code

 @Override
 public void onStart() {
    super.onStart();

  mEntityDataProvider = new EntityDataProvider();
  mEntityDataProvider = mEntityDataProvider.getListAsync();    
  mEntityDataProvider.addChangeListener(realmEntityChangeListener);

 }

 private RealmChangeListener<RealmResults<Entity>> realmEntityChangeListener = new RealmChangeListener<RealmResults<Entity>>() {
    @Override
    public void onChange(RealmResults<Entity> realmResults) {
        if (mEntityListAdapter!= null) {
            mEntityListAdapter.setData(realmResults);
            mEntityListAdapter.notifyDataSetChanged();
        }
    }
};

Questions:

  1. Is it best practice to call notifyDataSetChanged() in
    reamlChangeListner?
  2. For every commitTransaction() i think realmChangeListener will be called, calling notifyDataSetChanged() calling many times is it fine?
  3. If above practice is not good to do, suggest me if any alternatives I need to consider.

Thanks

Aucun commentaire:

Enregistrer un commentaire