问题
I have a set of data in a Realm that is displayed in a RecyclerView using a RealmRecyclerViewAdapter. Basically, I'll have 25 elements displayed in the RecyclerView -- if items are being inserted into the Realm for the first time there are no issues. It seems to work properly if the Realm isn't being added to shortly after all its data has been deleted because the issue only arises when I need to delete all the items in the Realm and then add a new Collection of items shortly afterwards.
What will happen is I will send an http request for the new items, and on receiving them, I'll delete all the items currently in the Realm, and then add the new ones, both in different transactions; I've also tried doing it in the same transaction.
The unexpected behavior is that the contents of the RecyclerView will flicker for a very brief moment, as if all the items are about to be rebound, and I can even see the data of the new items being shown for a split second, but then the contents of the RecyclerView just goes back to showing the previous data -- the data I know to be deleted from the Realm. I know it's deleted because if I interact with the RecyclerView after this flicker, even just a click on a static view, all the items will be instantly rebound and reflect the new items that have been added; as if notifyDataSetChanged() is finally being called.
This behavior isn't always exhibited. Most of the time the items are all deleted, the new ones are added, and it just works perfectly; no flicker. But as far as the user behavior goes when it works properly and when it doesn't, there is nothing different. The same thing can be done over and over again and sometimes it will work and other times it won't.
I've tried deleting the items using mRealm.deleteAll() and mOrderedRealmList.deleteAllFromRealm(). And I've tried adding the items to the collection using both mRealm.insert(items) and mRealm.copyToRealm(items). I've also tried manually calling notifyDataSetChanged() only after the items are added, only after the items are all deleted, and after both events, but the issue still occurs.
Any idea on what could be causing this behavior?
This is the only code that is run that manipulates Realm data:
mRealm.beginTransaction();
mOrderedRealmCollection().deleteAllFromRealm();
mRealm.commitTransaction();
...
...
processing of new items...
...
...
mRealm.beginTransaction();
mRealm.insert(items);
mRealm.commitTransaction();
Update:
When I delete all the items from the Realm and add the new items within the same transaction the unexpected behavior occurs more frequently. Also, I've noticed that when the changes are made in the same transaction and the issue occurs, sometimes there is no flicker, the RecyclerView just continues to display the old data, uninterrupted, but if I touch the RecyclerView anywhere, just like when the flicker does occur, all the items will be instantly rebound and reflect the new items that have been added.
Update: How Adapter is Set Up
//in onCreate() method
mItems = mRealm.where(Item.class).findAllSorted("mIndex", Sort.ASCENDING);
//in onCreateView() method
mRecyclerView.setAdapter(mAdapter = new MyRealmRecyclerViewAdapter(this, mItems));
Update: Cancel Button onClick() Code
private void setListeners(){
mCancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSearchEditText.clearFocus();
mCancelButton.setVisibility(View.GONE);
mCreateItemButton.setVisible(true);
Toast.makeText(A_Main.this,"Cancel clicked",Toast.LENGTH_SHORT).show();
}
});
来源:https://stackoverflow.com/questions/39084859/notifydatasetchanged-not-being-called-when-underlying-realm-is-changed-using-r