Multiple errors occuring when deleting an item from REALM in a recycler view.
Hi guys!
Im having an annoying issue with REALM and a RecyclerView in android. I made a simple project to illustrate this issue https://github.com/JamesDeegan/RealmRecyclerBug
The error crops up when rapidly deleting items from realm. A simple way to get an error if you clone my app is to use two fingers and push the delete button on two items at the same time.
The errors range from Process: realmrecyclerbug.com.realmrecyclerbug, PID: 8615 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{1257789 position=3 id=-1, oldPos=4, pLpos:4 scrap [attachedScrap] tmpDetached no parent}
to
java.lang.ArrayIndexOutOfBoundsException: rowIndex is less than 0.
I have searched the net for days now and the best "fix" i can find is to overide the GridLayoutManagers onLayoutChildren method like so.
@Override public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) { try { super.onLayoutChildren(recycler, state); } catch (IndexOutOfBoundsException e) { Log.e("probe", "meet a IOOBE in RecyclerView"); } }
This is not the best fix, as even if i call notifyDataSetChanged in the catch block there are still times when the adapter doesnt match the realm list (ie realm will contain no items, but the recycler view has one or two dead views that dont react to clicks)..
Please help!
So this happens when RealmRecyclerViewAdapter is used against a RealmList field of an object, and not a RealmResults. ( https://github.com/JamesDeegan/RealmRecyclerBug/blob/master/app/src/main/java/realmrecyclerbug/com/realmrecyclerbug/MainActivity.java#L161-L176 ) I think https://github.com/realm/realm-java/pull/4368 will fix this, but using asynchronous transaction instead of synchronous transaction should also work
I think async transactions fixed it. OMW! thank you so much my man. This is the end of a 1 week long problem. Thank you. I will test to see if the data is all correctly displayed and theres no dirty views. I got to go to work now. But from a 5 minuite test i think its solved. Champ! thanks bro
Yes, #4368 will fix this for the synchronized transaction case. @JamesDeegan BTW, please close the Realm instance after using it, eg.: in ItemAdapter.deleteItem() . See https://realm.io/docs/java/latest/#controlling-the-lifecycle-of-realm-instances
🎉