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");
//notifyDataSetChanged();
}
}
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!