From 425b35a2c1073d816e376c23c4ddf89d965aeb27 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 29 Mar 2018 10:50:06 -0400 Subject: [PATCH] Crash/UI fix fetching edge case This could result in either not fetching the remaining data or a crash depending on the exact shape of your data. // FREEBIE --- .../src/ViewControllers/MediaGalleryViewController.swift | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Signal/src/ViewControllers/MediaGalleryViewController.swift b/Signal/src/ViewControllers/MediaGalleryViewController.swift index d5278c558..c866da1a8 100644 --- a/Signal/src/ViewControllers/MediaGalleryViewController.swift +++ b/Signal/src/ViewControllers/MediaGalleryViewController.swift @@ -649,8 +649,13 @@ class MediaGalleryViewController: UINavigationController, MediaGalleryDataSource } let unfetchedSet = requestSet.subtracting(self.fetchedIndexSet) - guard unfetchedSet.count > (requestSet.count / 2) else { - // For perf we only want to fetch a relatively full batch, unless the requestSet is very small. + + // For perf we only want to fetch a substantially full batch... + let isSubstantialRequest = unfetchedSet.count > (requestSet.count / 2) + // ...but we always fulfill even small requests if we're getting just the tail end of a gallery. + let isFetchingEdgeOfGallery = (self.fetchedIndexSet.count - unfetchedSet.count) < requestSet.count + + guard isSubstantialRequest || isFetchingEdgeOfGallery else { Logger.debug("\(self.logTag) in \(#function) ignoring small fetch request: \(unfetchedSet.count)") return }