【问题标题】:Performing a Segue Inside of a Completion Handler在完成处理程序内部执行 Segue
【发布时间】:2015-12-16 06:46:50
【问题描述】:

我正在尝试在完成处理程序中执行情节提要,如下所示:

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

并收到以下警告:

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

完成处理程序在后台运行,所以我明白为什么会出现此错误,我的问题是我有哪些选项可以在不出现此错误的情况下执行此 segue?

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

【问题讨论】:

  • 我认为很容易用谷歌搜索 0.90% 的问题已经得到了完美的回答。

标签: ios swift


【解决方案1】:

每当您在 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)

     })            
})

【讨论】:

  • 所有答案都有帮助,所以谢谢大家,但是您使用了我提供的代码 swift,并建议使用弱自我,所以这个答案对我帮助最大。谢谢!
【解决方案2】:

放入调度队列:

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

【讨论】:

    【解决方案3】:

    此错误表明您正在从后台线程执行一些 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    
    }
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-16
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      相关资源
      最近更新 更多