在完成处理程序内部执行Segue

时间:2015-12-16 06:46:50

标签: ios swift

我正试图在完成处理程序中执行故事板segue,如下所示:

movieWriter.finishRecordingWithCompletionHandler({ () -> Void in
            //Leave this view
            self.performSegueWithIdentifier("decisionSegue", sender: self)
        })

并收到以下警告:

  

此应用程序正在从后台线程修改autolayout引擎,这可能导致引擎损坏和奇怪的崩溃。这将在将来的版本中引发异常。

完成处理程序在后台运行,所以我理解为什么我收到此错误,我的问题是我可以选择执行此segue而不会出现此错误?

我在完成处理程序中执行segue的原因是在完成记录的影片写入文件并且视图被视图播放电影后调用完成处理程序,因此它需要在之前存档segueing。

3 个答案:

答案 0 :(得分:4)

把它放在调度队列中:

dispatch_async(dispatch_get_main_queue(),{
    self.performSegueWithIdentifier("decisionSegue", sender: self)
})

希望它能运作

有关详细信息:This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes

答案 1 :(得分:4)

每当您在UI /活动视图上执行任何操作时,它必须位于主线程而不是后台线程。

执行以下操作:

__weak typeof(self) weakSelf = self; //Best practice
                                     //Provide a weak reference in block and not strong.

movieWriter.finishRecordingWithCompletionHandler({ () -> Void in

     dispatch_async(dispatch_get_main_queue(),{

        weakSelf.performSegueWithIdentifier("decisionSegue", sender:weakSelf)

     })            
})

答案 2 :(得分:2)

此错误告诉您正在从后台线程执行某些UI更新任务,并且无法从后台线程更新UI,因此您必须访问主线程然后执行segue。

目标-C:

dispatch_async(dispatch_get_main_queue(), ^{
    // update some UI
    // Perform your Segue here
});

夫特:

DispatchQueue.main.async {
    // update some UI
    // Perform your Segue here    
}

希望它会对你有所帮助。

相关问题