为什么 popViewController Animated 花了这么长时间才达到 运行?
Why is popViewControllerAnimated taking so long to run?
我有一个辅助 viewController 可以让我从相机胶卷中删除图像。问题是,completionHandler 像预期的那样触发,但 popViewController 实际上似乎 运行 大约 8 秒。它肯定会触发,因为我可以看到可选输出。我检查了只是做 pop,它 运行s 正确。我检查了 viewWillDisapear 事件,它也很晚才触发,考虑到导航控制器尚未弹出当前视图 viewController,我预计会发生这种情况。
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
})
文档是这么说的:
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.
导航控制器需要从主线程执行,所以你需要将调用包装成类似
的东西
dispatch_async(dispatch_get_main_queue()) {
navigationController.popViewControllerAnimated(true)
}
对于Swift3
DispatchQueue.main.async() {
self.navigationController?.popViewController(animated: true)
}
我有一个辅助 viewController 可以让我从相机胶卷中删除图像。问题是,completionHandler 像预期的那样触发,但 popViewController 实际上似乎 运行 大约 8 秒。它肯定会触发,因为我可以看到可选输出。我检查了只是做 pop,它 运行s 正确。我检查了 viewWillDisapear 事件,它也很晚才触发,考虑到导航控制器尚未弹出当前视图 viewController,我预计会发生这种情况。
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
})
文档是这么说的:
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.
导航控制器需要从主线程执行,所以你需要将调用包装成类似
的东西dispatch_async(dispatch_get_main_queue()) {
navigationController.popViewControllerAnimated(true)
}
对于Swift3
DispatchQueue.main.async() {
self.navigationController?.popViewController(animated: true)
}