Luis Felipe
Luis Felipe

Reputation: 990

Activity indicator acting slow inside PHPhotoLibrary.sharedPhotoLibrary().performChanges

I'm deleting some photo assets and I want to show an activity indicator while the assets are being deleted and stop it when the assets have been deleted but my code is acting 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")
                }
        })

Upvotes: 3

Views: 391

Answers (1)

Luis Felipe
Luis Felipe

Reputation: 990

I solved it myself, here's 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()
                }
        })

Just needed to add:

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

Upvotes: 3

Related Questions