lisawray / groupie

Groupie helps you display and manage complex RecyclerView layouts.
MIT License
3.66k stars 293 forks source link

IndexOutOfBoundsException: Wanted item at 5 but there are only 5 items #398

Closed vovan888 closed 3 years ago

vovan888 commented 3 years ago

Describe the bug

I my RecyclerView Adapter I use updateAsync(groups); to update all items. But sometimes it crashes with the following trace (taken from the Crashlytics):


Fatal Exception: java.lang.RuntimeException: java.lang.IndexOutOfBoundsException: Wanted item at 5 but there are only 5 items
       at com.xwray.groupie.DiffTask.onPostExecute(DiffTask.java:54)
       at com.xwray.groupie.DiffTask.onPostExecute(DiffTask.java:18)
       at android.os.AsyncTask.finish(AsyncTask.java:695)
       at android.os.AsyncTask.access$600(AsyncTask.java:180)
       at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:193)
       at android.app.ActivityThread.main(ActivityThread.java:6718)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by java.lang.IndexOutOfBoundsException: Wanted item at 5 but there are only 5 items
       at com.xwray.groupie.GroupUtils.getItem(GroupUtils.java:20)
       at com.xwray.groupie.DiffCallback.areItemsTheSame(DiffCallback.java:33)
       at androidx.recyclerview.widget.DiffUtil.backward(DiffUtil.java:300)
       at androidx.recyclerview.widget.DiffUtil.midPoint(DiffUtil.java:202)
       at androidx.recyclerview.widget.DiffUtil.calculateDiff(DiffUtil.java:146)
       at com.xwray.groupie.DiffTask.doInBackground(DiffTask.java:44)
       at com.xwray.groupie.DiffTask.doInBackground(DiffTask.java:18)
       at android.os.AsyncTask$2.call(AsyncTask.java:333)
       at java.util.concurrent.FutureTask.run(FutureTask.java:266)
       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
       at java.lang.Thread.run(Thread.java:764)

To Reproduce I cannot reproduce it myself. IMO it happens when an item is deleted from the list.

Library version groupie:2.8.1 androidx.recyclerview:recyclerview:1.2.1

vovan888 commented 3 years ago

Found root cause of this crash in my code - double notification of changed items when last item in a group is deleted.