I need to show progress of deleting photos and videos from photo library. In case of few photos - deletion can take few seconds but in case of 1000 photos - it will take few minutes. So, i need to show a progress somehow. How can I do that?
My code
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.deleteAssets(assets as NSFastEnumeration)
}, completionHandler: { success, error in
DispatchQueue.main.async {
if let error = error {
print(error.localizedDescription)
}
}
})
You can register a
PHPhotoLibraryChangeObserverto, as the name suggests, observe changes to the photo library. You can read more about it here.When the
PHPhotoLibraryChangeObservernotices a change, you can then request the details for that change to find out what to do, in this case you'd upload your user interface.You can read more about
PHPhotoLibraryChangeObserverhere.EDIT: As a response to the comment stating that this observer doesn't fire often enough for UI state updates, you could try a different approach, like querying all photos periodically (maybe every few seconds on a repeating timer) and then getting the count of the result. Then when the operation (in this case, deleting the items) is finished you can invalidate the timer.