ra1028 / DifferenceKit

💻 A fast and flexible O(n) difference algorithm framework for Swift collection.

Home Page:https://ra1028.github.io/DifferenceKit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Buggy default reload method

gerchicov-bp opened this issue · comments

Checklist

Expected Behavior

Table reloads

Current Behavior

Table reloading failed with exception:
Thread 1: "attempt to insert row 0 into section 1, but there are only 0 rows in section 1 after the update"

Steps to Reproduce

Code example:

class Item { ... }
class Group {
var isExpanded = true
var items: [Item] = ...
func visibleItems() -> [Item] { isExpanded ? self.items : [] }
...
}
var groups: [Group] = {
  let group = Group()
  group.items = Item()
  return [group]
}()

Where groups represent sections and items represent rows. Just expand-collapse a section (isExpanded changed), calc the diff and try to reload the table with these data using any method in this library.

Detailed Description (Include Screenshots)

The problem is in your reload method which attempts to reload data step-by-step. In my case it means on expand it attempts to add one Item and insert it into the table immediately ignoring isExpanded value which should be updated in the next run loop.

Reproducible Demo Project

I don't have but it seems the task is trivial to reproduce.

Environments

Doesn't matter because there is an error inside this library algo.

It seems the problem is with this code:

// The 4th stage changeset.
        // - Includes:
        //   - element inserts
        //   - element moves
        if !elementInserted.isEmpty || !elementMoved.isEmpty {
            changesets.append(
                Changeset(
                    data: Collection(fourthStageSections),
                    elementInserted: elementInserted,
                    elementMoved: elementMoved
                )
            )
        }

        // The 5th stage changeset.
        // - Includes:
        //   - section updates
        if !sectionResult.updated.isEmpty {
            changesets.append(
                Changeset(
                    data: target,
                    sectionUpdated: sectionResult.updated
                )
            )
        }

In my case element inserts and section updates should be inside the same changeset, not separate changesets. So it seems it is a problem of your algo.