IGListKit/Source/IGListDiffKit
Maxime Ollivier ba2b3150b0 fix crash when reloading the same cell index multiple times
Summary:
## Context
Merging multiple batch updates of the same section is inherently unsafe, because each block can mutate the underlaying list and lead to insert/deletes that operate on different before/after lists. The long term fix is to get rid of item level batch updates (insert/delete calls) and use diffing like for sections, but that's a much larger project.

Right now, we have a crash spiking because we're reloading the same cell multiple times. Each time `IGListDiff(...)` returns a pair of insert/delete for the same index (lets say 0). Once these updates are merge together, they look like this:
```
Item inserts = 0, 0
Item deletes = 0, 0
```
We dedupe deletes in `IGListBatchUpdateData`, but don't do anything for inserts, so it looks liket this:

```
Item inserts = 0, 0
Item deletes = 0
```

So we create an insert/delete unbalance and can crash. Tbh, not sure how this hasn't been a bigger issue in the past.

D21822502 tried to remove all duplicate inserts, but that caused other types of crashes, because multiple inserts are allowed.

## This diff

* Lets keep the net insert/delete count the same by keeping track of the number of deletes we remove and apply the same to inserts.
* Add some tests
* Add an experiment flag to kill this if needed to be safe. IG only: Note that I'm making the default true because the crash is happening pretty at start-up.

## Follow up

Over time, the batch update clean up logic was slowly added to multiple place (IGListBatchUpdateData, IGListAdapterUpdaterHelpers, IGListIndexPathResult), which is making it hard to follow. A first step would be to move it  in 1 or 2 places and avoid redundant logic (like getting unique deletes). However, the real fix is getting rid of batch updates all together.

Differential Revision: D61719884

fbshipit-source-id: eda7c264c8239a6a106dbe0256fe777b38fae335
2025-12-10 17:18:12 +09:00
..
Internal Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
IGListAssert.h Fix IGWarnAssert macro to remove unneeded condition parameter 2025-12-10 17:18:10 +09:00
IGListBatchUpdateData.h fix crash when reloading the same cell index multiple times 2025-12-10 17:18:12 +09:00
IGListBatchUpdateData.mm fix crash when reloading the same cell index multiple times 2025-12-10 17:18:12 +09:00
IGListCompatibility.h Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
IGListDiff.h Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
IGListDiff.mm Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
IGListDiffable.h Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
IGListDiffKit.h Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
IGListExperiments.h fix crash when reloading the same cell index multiple times 2025-12-10 17:18:12 +09:00
IGListIndexPathResult.h Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
IGListIndexPathResult.m Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
IGListIndexSetResult.h Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
IGListIndexSetResult.m Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
IGListMacros.h Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
IGListMoveIndex.h Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
IGListMoveIndex.m Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
IGListMoveIndexPath.h Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
IGListMoveIndexPath.m Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
NSNumber+IGListDiffable.h Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
NSNumber+IGListDiffable.m Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
NSString+IGListDiffable.h Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00
NSString+IGListDiffable.m Standarize the copyright notice in all source files 2025-12-10 17:18:09 +09:00