Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple errors occuring when deleting an item from REALM in a recycler view. #4384

Closed
JamesDeegan opened this issue Mar 25, 2017 · 3 comments

Comments

@JamesDeegan
Copy link

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!

@Zhuinden
Copy link
Contributor

Zhuinden commented Mar 25, 2017

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 #4368 will fix this, but using asynchronous transaction instead of synchronous transaction should also work

@JamesDeegan
Copy link
Author

JamesDeegan commented Mar 25, 2017

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

@beeender
Copy link
Contributor

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
🎉

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants