Search code examples
iosswiftphotokit

Why is popViewControllerAnimated taking so long to run?


I have a secondary viewController that allows me to delete images from the camera roll. The problem is, the completionHandler fires like it's suppose to, but the popViewController doesn't actually seem to run for about 8 seconds. It definitely fires, because I can see the optional output. And I checked just doing the pop, and it runs correctly. I checked the viewWillDisapear event, and it fires late as well, which I expected considering the nav controller hadn't popped the view current viewController yet.

PHPhotoLibrary.sharedPhotoLibrary().performChanges({

    PHAssetChangeRequest.deleteAssets(assetsToDelete)
        return
    }, completionHandler: { success, error in
       if success {
           println("success")
           println(navigationController.popViewControllerAnimated(true))
           println("so slow")
       }
       if let error = error {
           println(error)
       }
       return
})

Solution

  • This is what the documentation says:

    Photos executes both the change block and the completion handler block on an arbitrary serial queue. To update your app’s UI as a result of a change, dispatch that work to the main queue.

    The navigation controller needs to be executed from the main thread, so you need to wrap the call to something like

    dispatch_async(dispatch_get_main_queue()) { 
        navigationController.popViewControllerAnimated(true)
    }