2

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!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
MrRed
  • 677
  • 1
  • 6
  • 21
  • I mentioned this on the github issues, but I think Realm 3.1 will fix this by forcing `RealmChangeListener` to be called synchronously on `realm.commitTransaction()`. Btw a possible workaround (to force the unreleased 3.1 behavior) would be `realm.sharedRealm.refresh()` as per [**this answer**](http://stackoverflow.com/a/38839808/2413303) after calling `realm.commitTransaction()`, can you please try? – EpicPandaForce Mar 25 '17 at 22:38
  • Hey bro! i tried it but i cant access sharedRealm it says it has protected access? – MrRed Mar 28 '17 at 10:53
  • the package is important in that answer, the `RealmRefresh` class needs to be in `package io.realm;` – EpicPandaForce Mar 28 '17 at 14:04
  • Ok i did it!!! https://pastebin.com/ZNdMeBjx This solution works. On double tap (delete two different items) it works! Cheers bud. Ill stick to the async method though. Anyway i hope this helps you guys out – MrRed Mar 28 '17 at 14:54
  • Next version will fix this :) – EpicPandaForce Mar 28 '17 at 16:42

0 Answers0