The activity indicator is slow inside PHPhotoLibrary.sharedPhotoLibrary (). PerformChanges

I am deleting some photo assets and I want to show an activity indicator when assets are being deleted and stop it when assets have been deleted, but my code is slow, do you know what is wrong?

PHPhotoLibrary.sharedPhotoLibrary().performChanges({
            PHAssetChangeRequest.deleteAssets(enumeration)
            self.activityIndicator.startAnimating()
            UIApplication.sharedApplication().beginIgnoringInteractionEvents()
            }, completionHandler: {success, error in
                if success {
                    self.activityIndicator.stopAnimating()
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                    println("Success")
                } else {
                    self.activityIndicator.stopAnimating()
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                    println("Error")
                }
        })

      

+3


source to share


1 answer


I solved it myself, here is the answer:

PHPhotoLibrary.sharedPhotoLibrary().performChanges({
            PHAssetChangeRequest.deleteAssets(enumeration)

            let delay = 1 * Double(NSEC_PER_SEC)
            let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
            dispatch_after(time, dispatch_get_main_queue()) {
                self.activityIndicator.startAnimating()
            }
            }, completionHandler: {success, error in

                UIApplication.sharedApplication().beginIgnoringInteractionEvents()

                if success {
                    println("good")
                    dispatch_async(dispatch_get_main_queue()){
                        self.activityIndicator.stopAnimating()
                        self.navigationController?.popToRootViewControllerAnimated(true)
                    }
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                } else {
                    println("bad")
                    dispatch_async(dispatch_get_main_queue()){
                        self.activityIndicator.stopAnimating()
                    }
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                }
        })

      



You just need to add:

dispatch_async(dispatch_get_main_queue()){
     self.activityIndicator.stopAnimating()
}

      

+3


source







All Articles