Improve scroll-jank on Gallery Tile View

- Don't fetch data unnecessarily
- Use CATransaction to avoid insertion animations. They are off screen
  anyway.

// FREEBIE
pull/1/head
Michael Kirk 7 years ago
parent fa6d9bfb38
commit 19988a872a

@ -340,15 +340,20 @@ public class MediaTileViewController: UICollectionViewController, MediaGalleryCe
return
}
guard !mediaGalleryDataSource.hasFetchedOldest else {
return
}
guard !isFetchingMoreData else {
Logger.debug("\(logTag) in \(#function) already fetching more data")
return
}
isFetchingMoreData = true
let scrollDistanceToBottom = oldContentHeight - contentOffsetY
CATransaction.begin()
CATransaction.setDisableActions(true)
collectionView.performBatchUpdates({
mediaGalleryDataSource.ensureGalleryItemsLoaded(.before, item: oldestLoadedItem, amount: self.kMediaTileViewLoadBatchSize) { addedSections, addedItems in
Logger.debug("\(self.logTag) in \(#function) insertingSections: \(addedSections) items: \(addedItems)")
@ -365,7 +370,9 @@ public class MediaTileViewController: UICollectionViewController, MediaGalleryCe
Logger.debug("\(self.logTag) in \(#function) performBatchUpdates finished: \(finished)")
self.isFetchingMoreData = false
CATransaction.commit()
})
} else if oldContentHeight - contentOffsetY < kEdgeThreshold {
// Near the bottom, load newer content
@ -374,22 +381,31 @@ public class MediaTileViewController: UICollectionViewController, MediaGalleryCe
return
}
guard !mediaGalleryDataSource.hasFetchedMostRecent else {
return
}
guard !isFetchingMoreData else {
Logger.debug("\(logTag) in \(#function) already fetching more data")
return
}
isFetchingMoreData = true
collectionView.performBatchUpdates({
mediaGalleryDataSource.ensureGalleryItemsLoaded(.after, item: mostRecentLoadedItem, amount: self.kMediaTileViewLoadBatchSize) { addedSections, addedItems in
Logger.debug("\(self.logTag) in \(#function) insertingSections: \(addedSections), items: \(addedItems)")
collectionView.insertSections(addedSections)
collectionView.insertItems(at: addedItems)
}
}, completion: { finished in
Logger.debug("\(self.logTag) in \(#function) performBatchUpdates finished: \(finished)")
self.isFetchingMoreData = false
})
CATransaction.begin()
CATransaction.setDisableActions(true)
UIView.performWithoutAnimation {
collectionView.performBatchUpdates({
mediaGalleryDataSource.ensureGalleryItemsLoaded(.after, item: mostRecentLoadedItem, amount: self.kMediaTileViewLoadBatchSize) { addedSections, addedItems in
Logger.debug("\(self.logTag) in \(#function) insertingSections: \(addedSections), items: \(addedItems)")
collectionView.insertSections(addedSections)
collectionView.insertItems(at: addedItems)
}
}, completion: { finished in
Logger.debug("\(self.logTag) in \(#function) performBatchUpdates finished: \(finished)")
self.isFetchingMoreData = false
CATransaction.commit()
})
}
}
}

Loading…
Cancel
Save