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?