RdB
RdB

Reputation: 65

Realm is crashing iOS app: 'RLMException', reason: 'Index 38 is out of bounds (must be less than 38).'

I'm writing an iOS app (iOs 14.4.2, Xcode 12.5.1, Realm 5.2.0).

One of the tables I'm using contains cells generated from a "Results" array. The app loads up fine and populates the table correctly with the correct number of cells, correct information, etc.

The problem occurs when I remove a cell from the table (using UISwipeActionsConfiguration). When I print out the number of objects in the results array before and after the action, the number decreases as expected. (For example, if I started with 39 items, it decreases to 38). As long as the bottom of the table is not in view, everything is fine. However, when I scroll to the end of the table, as soon as the final cell is rendered in the UI, the app crashes with an "RLM exception: index is out bounds" error. The stack track shows that the error is being thrown from the "cellForRowAt method". The strange thing is that the realm is saying that the results array should contain 1 LESS than the number of objects shown to be contained in the Results Array.

Does anyone have any idea why this could be? Am I missing something really basic?

Example 1:

  1. Load up table with 40 items. (Results array shows 40 objects). Final row is not yet visible and app functions normally.
  2. Swipe to delete item at index 10. (Results array shows 39 objects) Final row is not yet visible and app functions normally.
  3. Scroll down to end of table. App crashes. Error message: Terminating app due to uncaught exception 'RLMException', reason: 'Index 38 is out of bounds (must be less than 38).

Example 2:

  1. Load up table with 10 items. (Results array shows 10 objects) Final row is visible and app functions normally.
  2. Swipe to delete item at index 2. (Results array shows 9 objects)
  3. App crashes. Error message: Terminating app due to uncaught exception 'RLMException', reason: 'Index 8 is out of bounds (must be less than 8).

2 Questions:

  1. Why is it saying that the index of the final object is out of bounds when it also shows that the updated array contains that exact number of objects?
  2. And why does it say that the index must be less than the number of objects in the updated array?
  3. Most importantly - does anyone know what the issue could be?

I am running multiple tables in the UI concurrently with the same information presented in different orders based on different parameters, but based on this thread (https://github.com/realm/realm-cocoa/issues/4499), that should not be an issue. (I even tried to hide the other tables, so that only one table is displayed, but the results are the same).

Any help would be greatly appreciated!

Photo of the backtrace, if it helps:

enter image description here

Upvotes: 1

Views: 491

Answers (1)

RdB
RdB

Reputation: 65

I found my issue. I was not updating (and reloading ALL the tables) with the new realm information - and this was important - immediately after Step 2 in both examples (i.e. as part of the same action). Smh.

Upvotes: 0

Related Questions