Respond to CR.

pull/1/head
Matthew Chen 7 years ago
parent 6c38f8d14c
commit 63aa71c318

@ -48,19 +48,22 @@ class PhotoPickerAssetItem: PhotoGridItem {
func asyncThumbnail(completion: @escaping (UIImage?) -> Void) -> UIImage? { func asyncThumbnail(completion: @escaping (UIImage?) -> Void) -> UIImage? {
var syncImageResult: UIImage? var syncImageResult: UIImage?
var hasCompleted = false var hasLoadedImage = false
// Surprisingly, iOS will opportunistically run the completion block sync if the image is // Surprisingly, iOS will opportunistically run the completion block sync if the image is
// already available // already available.
photoCollectionContents.requestThumbnail(for: self.asset, thumbnailSize: photoMediaSize.thumbnailSize) { image, _ in photoCollectionContents.requestThumbnail(for: self.asset, thumbnailSize: photoMediaSize.thumbnailSize) { image, _ in
DispatchMainThreadSafe({ DispatchMainThreadSafe({
syncImageResult = image syncImageResult = image
// Once we've _successfully_ completed (e.g. invoked the completion with // Once we've _successfully_ completed (e.g. invoked the completion with
// a non-nil image), don't invoke the completion again. // a non-nil image), don't invoke the completion again with a nil argument.
if !hasCompleted { if !hasLoadedImage || image != nil {
completion(image) completion(image)
hasCompleted = image != nil
if image != nil {
hasLoadedImage = true
}
} }
}) })
} }

Loading…
Cancel
Save