From 13007d0acbcbc198bc6bbaa90c1dc89f3b83f912 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Tue, 24 May 2016 23:13:34 +0100 Subject: [PATCH] Fixes bug with GridView reloading unexpectedly. --- Memories/Grid View/GridViewModel.swift | 27 +++++++++---------- Memories/Photo View/PhotoViewController.swift | 26 +++++++++++++++--- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/Memories/Grid View/GridViewModel.swift b/Memories/Grid View/GridViewModel.swift index b954213..865510b 100644 --- a/Memories/Grid View/GridViewModel.swift +++ b/Memories/Grid View/GridViewModel.swift @@ -12,13 +12,7 @@ import PHAssetHelper class GridViewModel { private let assetHelper = PHAssetHelper() - private var assetFetchResults = [PHFetchResult]() { - didSet { - dispatch_async(dispatch_get_main_queue()) { [unowned self] in - self.onDataChanged(self.date.value) - } - } - } + private var assetFetchResults = [PHFetchResult]() let date : Dynamic let onDataChanged: (NSDate) -> () @@ -33,10 +27,7 @@ class GridViewModel { self.onDataChanged = onDataChanged self.date = Dynamic(NSDate()) self.date.bind { - let date = $0 - dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { [unowned self] in - self.assetFetchResults = self.assetHelper.fetchResultsForDateInAllYears(date) - } + self.fetchDataAndNotify($0) } } @@ -44,13 +35,19 @@ class GridViewModel { self.onDataChanged = onDataChanged self.date = Dynamic(date) self.date.bindAndFire { - let date = $0 - dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { [unowned self] in - self.assetFetchResults = self.assetHelper.fetchResultsForDateInAllYears(date) - } + self.fetchDataAndNotify($0) } } + private func fetchDataAndNotify(date: NSDate) { + dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { [unowned self] in + self.assetFetchResults = self.assetHelper.fetchResultsForDateInAllYears(date) + dispatch_async(dispatch_get_main_queue()) { [unowned self] in + self.onDataChanged(self.date.value) + } + } + } + // MARK: - API func goToNextDay() { date.value = date.value.nextDay() diff --git a/Memories/Photo View/PhotoViewController.swift b/Memories/Photo View/PhotoViewController.swift index c274560..76e451a 100644 --- a/Memories/Photo View/PhotoViewController.swift +++ b/Memories/Photo View/PhotoViewController.swift @@ -141,8 +141,10 @@ class PhotoViewController: UIViewController, UIScrollViewDelegate, UIViewControl @IBAction func close(sender: UIButton) { if let navController = presentingViewController as? UINavigationController, gridViewController = navController.topViewController as? GridViewController { + cancelAllImageRequests() + PHPhotoLibrary.sharedPhotoLibrary().unregisterChangeObserver(self) + gridViewController.setSelectedIndex(model.selectedIndex) - if traitCollection.verticalSizeClass == .Regular { hideStatusBar(false) } @@ -268,7 +270,7 @@ class PhotoViewController: UIViewController, UIScrollViewDelegate, UIViewControl options.deliveryMode = .HighQualityFormat options.synchronous = false - let requestId = PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFit, options: options) { (result, userInfo) -> Void in + pageView.imageRequestId = PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFit, options: options) { (result, userInfo) -> Void in if let image = result { UpgradeManager.highQualityViewCount += 1 pageView.image = image @@ -289,8 +291,6 @@ class PhotoViewController: UIViewController, UIScrollViewDelegate, UIViewControl pageView.fullImageUnavailable = true } } - - pageView.imageRequestId = requestId } func purgePage(page: Int) { @@ -302,6 +302,7 @@ class PhotoViewController: UIViewController, UIScrollViewDelegate, UIViewControl if let pageView = pageViews[page] { if let requestId = pageView.imageRequestId { PHImageManager.defaultManager().cancelImageRequest(requestId) + pageView.imageRequestId = nil } pageView.image = nil pageView.removeFromSuperview() @@ -309,6 +310,19 @@ class PhotoViewController: UIViewController, UIScrollViewDelegate, UIViewControl } } + func cancelPageImageRequest(page: Int) { + guard page >= 0 && page < pageViews.count else { + return + } + + if let pageView = pageViews[page] { + if let requestId = pageView.imageRequestId { + PHImageManager.defaultManager().cancelImageRequest(requestId) + pageView.imageRequestId = nil + } + } + } + func loadVisiblePages() { let initialLoad = !initialOffsetSet @@ -343,6 +357,10 @@ class PhotoViewController: UIViewController, UIScrollViewDelegate, UIViewControl model.assets.count.stride(to: lastPage, by: -1).forEach(purgePage) } + func cancelAllImageRequests() { + pageViews.indices.forEach(cancelPageImageRequest) + } + func purgeAllViews() { pageViews.indices.forEach(purgePage) }