2

This is my function to get all assets from PhotoLibrary.

// MARK: Album photos

    func getPhotos() {
        ImageManger.photoAuthorization { (granted: Bool) in
            if granted {
                // Asynchronous get photos, avoid taking the main thread
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
                    self.photoGroups = ImageManger.photoLibrarys()
                    if self.photoGroups?.count == 0 {
                        return
                    }

                    let group = self.photoGroups?[0]
                    self.photoAssets = ImageManger.photoAssetsForAlbum(group!)
                    for asset: PHAsset in self.photoAssets! {
                        ImageManger.imageFromAsset(asset, isOriginal: false, toSize: CGSize(width: 150, height: 150), resultHandler: { (image: UIImage?) in
                            guard image != nil else {
                                return
                            }
                            self.photos.append(image!)
                        })
                    }

                    dispatch_async(dispatch_get_main_queue(), {
                        // Reload data
                        self.albumCollection.reloadData()
                    })
                })
            } else {
                self.presentAccessRestrictedMessage()
            }
        }
    }

I call getPhotos() in viewWillAppear as it is in a TabViewController and I also call getPhotos when App enters foreground in order to sync assets if it is captured. It works pretty fine.

The problem is, when I have 500+ assets in PhotoLibrary, it takes couple of seconds to load. By the time, if I close the app and reopen again, or, if I switch tab and come back to the same controller, i.e, if I force getPhotos() to call, the photos array is messed up!

When the closure is still running, if I call getPhotos(), the array elements are duplicated. How could I handle this situation?

iOS
  • 3,526
  • 3
  • 37
  • 82
  • Why not just use `viewDidLoad` that only run once? Alternatively, you can also set a `bool` flag to check if `getPhotos()` is running. – Zac Kwan Nov 07 '16 at 12:31
  • 1
    Use `NSOperation`and `NSOperationQueue`instead of `dispatch...`. You can call `cancle()` on a `NSOperation`. See: http://stackoverflow.com/a/40462912/1457385 – shallowThought Nov 07 '16 at 12:32
  • 1
    Hi all, I've found this very useful. I hope resolve this question. http://jordansmith.io/cancelling-background-tasks/ – Trevor Dec 28 '18 at 11:11

0 Answers0