Skip to content

Commit

Permalink
Updated README.md with updateData documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
adya committed Aug 22, 2019
1 parent e164a7b commit 486d2d1
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,47 @@ However, if we decided to sort it so that deletions and higher indices are proce

### Table and Collection Views

```swift
// The following will automatically animate deletions, insertions, and moves:

tableView.animateRowChanges(oldData: old, newData: new)
The following will automatically animate deletions, insertions, and moves:

collectionView.animateItemChanges(oldData: old, newData: new)
```swift
tableView.animateRowChanges(oldData: old, newData: new)

// It can work with sections, too!
collectionView.animateItemChanges(oldData: old, newData: new, updateData: { self.dataSource = new })
```

It can work with sections, too!
```swift
tableView.animateRowAndSectionChanges(oldData: old, newData: new)

collectionView.animateItemAndSectionChanges(oldData: old, newData: new)
collectionView.animateItemAndSectionChanges(oldData: old, newData: new, updateData: { self.dataSource = new })
```

You can also calculate `diff` separately and use it later:
```swift
// Generate the difference first
let diff = dataSource.diff(newDataSource)

// This will apply changes to dataSource.
let dataSourceUpdate = { self.dataSource = newDataSource }

// ...

tableView.apply(diff)

collectionView.apply(diff, updateData: dataSourceUpdate)
```

Please see the [included examples](/Examples/) for a working sample.

#### Note about `updateData`

Since version `2.0.0` there is now an `updateData` closure which notifies you when it's an appropriate time to update `dataSource` of your `UICollectionView`. This addition refers to UICollectionView's [performbatchUpdates](https://developer.apple.com/documentation/uikit/uicollectionview/1618045-performbatchupdates):

> If the collection view's layout is not up to date before you call this method, a reload may occur. To avoid problems, you should update your data model inside the updates block or ensure the layout is updated before you call `performBatchUpdates(_:completion:)`.
Thus, it is **recommended** to update your `dataSource` inside `updateData` closure to avoid potential crashes during animations.

### Using Patch and Diff

When you want to determine the steps to transform one collection into another (e.g. you want to animate your user interface according to changes in your model), you could do the following:
Expand Down

0 comments on commit 486d2d1

Please sign in to comment.