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 8 years ago
parent fa6d9bfb38
commit 19988a872a

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

Loading…
Cancel
Save