Skip to content

Commit 5788cde

Browse files
committed
Allow user to update data source after performBatchUpdates is called and before updates are applied. Updating the data source in places otherwise causes a crash.
1 parent 2fa2874 commit 5788cde

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

Sources/Differ/Diff+UIKit.swift

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,11 @@ public extension UICollectionView {
258258
oldData: T,
259259
newData: T,
260260
indexPathTransform: @escaping (IndexPath) -> IndexPath = { $0 },
261+
updateData: ((T) -> Void)? = nil,
261262
completion: ((Bool) -> Void)? = nil
262263
) where T.Element: Equatable {
263264
let diff = oldData.extendedDiff(newData)
264-
apply(diff, completion: completion, indexPathTransform: indexPathTransform)
265+
apply(diff, data: newData, updateData: updateData, completion: completion, indexPathTransform: indexPathTransform)
265266
}
266267

267268
/// Animates items which changed between oldData and newData.
@@ -277,18 +278,22 @@ public extension UICollectionView {
277278
newData: T,
278279
isEqual: EqualityChecker<T>,
279280
indexPathTransform: @escaping (IndexPath) -> IndexPath = { $0 },
281+
updateData: ((T) -> Void)? = nil,
280282
completion: ((Bool) -> Swift.Void)? = nil
281283
) {
282284
let diff = oldData.extendedDiff(newData, isEqual: isEqual)
283-
apply(diff, completion: completion, indexPathTransform: indexPathTransform)
285+
apply(diff, data: newData, updateData: updateData, completion: completion, indexPathTransform: indexPathTransform)
284286
}
285287

286-
func apply(
288+
func apply<T>(
287289
_ diff: ExtendedDiff,
290+
data: T,
291+
updateData: ((T) -> Void)? = nil,
288292
completion: ((Bool) -> Swift.Void)? = nil,
289293
indexPathTransform: @escaping (IndexPath) -> IndexPath = { $0 }
290294
) {
291295
performBatchUpdates({
296+
updateData?(data)
292297
let update = BatchUpdate(diff: diff, indexPathTransform: indexPathTransform)
293298
self.deleteItems(at: update.deletions)
294299
self.insertItems(at: update.insertions)
@@ -309,15 +314,18 @@ public extension UICollectionView {
309314
newData: T,
310315
indexPathTransform: @escaping (IndexPath) -> IndexPath = { $0 },
311316
sectionTransform: @escaping (Int) -> Int = { $0 },
317+
updateData: ((T) -> Void)? = nil,
312318
completion: ((Bool) -> Swift.Void)? = nil
313319
)
314320
where T.Element: Collection,
315321
T.Element: Equatable,
316322
T.Element.Element: Equatable {
317323
self.apply(
318324
oldData.nestedExtendedDiff(to: newData),
325+
data: newData,
319326
indexPathTransform: indexPathTransform,
320327
sectionTransform: sectionTransform,
328+
updateData: updateData,
321329
completion: completion
322330
)
323331
}
@@ -337,6 +345,7 @@ public extension UICollectionView {
337345
isEqualElement: NestedElementEqualityChecker<T>,
338346
indexPathTransform: @escaping (IndexPath) -> IndexPath = { $0 },
339347
sectionTransform: @escaping (Int) -> Int = { $0 },
348+
updateData: ((T) -> Void)? = nil,
340349
completion: ((Bool) -> Swift.Void)? = nil
341350
)
342351
where T.Element: Collection,
@@ -346,8 +355,10 @@ public extension UICollectionView {
346355
to: newData,
347356
isEqualElement: isEqualElement
348357
),
358+
data: newData,
349359
indexPathTransform: indexPathTransform,
350360
sectionTransform: sectionTransform,
361+
updateData: updateData,
351362
completion: completion
352363
)
353364
}
@@ -367,6 +378,7 @@ public extension UICollectionView {
367378
isEqualSection: EqualityChecker<T>,
368379
indexPathTransform: @escaping (IndexPath) -> IndexPath = { $0 },
369380
sectionTransform: @escaping (Int) -> Int = { $0 },
381+
updateData: ((T) -> Void)? = nil,
370382
completion: ((Bool) -> Swift.Void)? = nil
371383
)
372384
where T.Element: Collection,
@@ -376,8 +388,10 @@ public extension UICollectionView {
376388
to: newData,
377389
isEqualSection: isEqualSection
378390
),
391+
data: newData,
379392
indexPathTransform: indexPathTransform,
380393
sectionTransform: sectionTransform,
394+
updateData: updateData,
381395
completion: completion
382396
)
383397
}
@@ -399,6 +413,7 @@ public extension UICollectionView {
399413
isEqualElement: NestedElementEqualityChecker<T>,
400414
indexPathTransform: @escaping (IndexPath) -> IndexPath = { $0 },
401415
sectionTransform: @escaping (Int) -> Int = { $0 },
416+
updateData: ((T) -> Void)? = nil,
402417
completion: ((Bool) -> Swift.Void)? = nil
403418
)
404419
where T.Element: Collection {
@@ -408,19 +423,24 @@ public extension UICollectionView {
408423
isEqualSection: isEqualSection,
409424
isEqualElement: isEqualElement
410425
),
426+
data: newData,
411427
indexPathTransform: indexPathTransform,
412428
sectionTransform: sectionTransform,
429+
updateData: updateData,
413430
completion: completion
414431
)
415432
}
416433

417-
func apply(
434+
func apply<T: Collection>(
418435
_ diff: NestedExtendedDiff,
436+
data: T,
419437
indexPathTransform: @escaping (IndexPath) -> IndexPath = { $0 },
420438
sectionTransform: @escaping (Int) -> Int = { $0 },
439+
updateData: ((T) -> Void)? = nil,
421440
completion: ((Bool) -> Void)? = nil
422441
) {
423442
performBatchUpdates({
443+
updateData?(data)
424444
let update = NestedBatchUpdate(diff: diff, indexPathTransform: indexPathTransform, sectionTransform: sectionTransform)
425445
self.insertSections(update.sectionInsertions)
426446
self.deleteSections(update.sectionDeletions)

0 commit comments

Comments
 (0)