Skip to content

Commit 188d8e1

Browse files
committed
fix(ios): prevent crash on ObservableArray change while the CollectionView is not “visible”
1 parent efa4777 commit 188d8e1

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/collectionview/index.ios.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,16 @@ export class CollectionView extends CollectionViewBase {
472472
// this.clearCellSize();
473473

474474
const sizes: NSMutableArray<NSValue> = this._delegate instanceof UICollectionViewDelegateImpl ? this._delegate.cachedSizes : null;
475+
const performBatchUpdatesCompletion = (c) =>
476+
{
477+
// if we are not "presented" (viewController hidden) then performBatchUpdatesCompletion would crash
478+
const viewIsLoaded = !!this.page?.viewController ? !!this.page.viewController.view.window : true;
479+
if (viewIsLoaded) {
480+
view.performBatchUpdatesCompletion(c, null);
481+
} else {
482+
c();
483+
}
484+
}
475485

476486
switch (event.action) {
477487
case ChangeType.Delete: {
@@ -487,9 +497,9 @@ export class CollectionView extends CollectionViewBase {
487497
if (Trace.isEnabled()) {
488498
CLog(CLogTypes.info, 'deleteItemsAtIndexPaths', indexes.count);
489499
}
490-
view.performBatchUpdatesCompletion(() => {
500+
performBatchUpdatesCompletion(() => {
491501
view.deleteItemsAtIndexPaths(indexes);
492-
}, null);
502+
});
493503
return;
494504
}
495505
case ChangeType.Update: {
@@ -503,9 +513,9 @@ export class CollectionView extends CollectionViewBase {
503513
CLog(CLogTypes.info, 'reloadItemsAtIndexPaths', event.index, indexes.count);
504514
}
505515

506-
view.performBatchUpdatesCompletion(() => {
516+
performBatchUpdatesCompletion(() => {
507517
view.reloadItemsAtIndexPaths(indexes);
508-
}, null);
518+
});
509519
return;
510520
}
511521
case ChangeType.Add: {
@@ -520,13 +530,13 @@ export class CollectionView extends CollectionViewBase {
520530
if (Trace.isEnabled()) {
521531
CLog(CLogTypes.info, 'insertItemsAtIndexPaths', indexes.count);
522532
}
523-
view.performBatchUpdatesCompletion(() => {
533+
performBatchUpdatesCompletion(() => {
524534
view.insertItemsAtIndexPaths(indexes);
525-
}, null);
535+
});
526536
return;
527537
}
528538
case ChangeType.Splice: {
529-
view.performBatchUpdatesCompletion(() => {
539+
performBatchUpdatesCompletion(() => {
530540
const added = event.addedCount;
531541
const removed = (event.removed && event.removed.length) || 0;
532542
if (added > 0 && added === removed) {
@@ -571,7 +581,7 @@ export class CollectionView extends CollectionViewBase {
571581
}
572582
}
573583
// view.collectionViewLayout.invalidateLayout();
574-
}, null);
584+
});
575585
return;
576586
}
577587
}

0 commit comments

Comments
 (0)