美文网首页iOS专栏
更新UI出错的解决方法

更新UI出错的解决方法

作者: TomatosX | 来源:发表于2016-01-24 23:45 被阅读2715次

最近的iOS项目中,遇到了一个奇怪的问题(好像是iOS 9上面才有),报错信息如下:

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.

问题出现在我截取视频成功之后需要调用重新载入视频的方法去更新页面上的信息,代码示例如下:

exportSession.exportAsynchronouslyWithCompletionHandler { () -> Void in
    switch (exportSession.status) {
        case AVAssetExportSessionStatus.Completed:
            print("Export Complete. Status:\(exportSession.status), Error: \(exportSession.error)")
            self!.reloadCutVideo(self!.videoCacheURL, pointType: pointType)
        case AVAssetExportSessionStatus.Failed:
            print("Failed: \(exportSession.error)")
        default:
            break;
    }
}

reloadCutVideo方法就是我需要调用来重新加载视频的方法。但是一调用就会出现上面的错误信息,而且debug发现,reloadCutVideo方法根本没有执行。

我后来试过用通知的方式调用方法,结果还是一样,最后终于想到,会不会是更新页面不能放在子线程中呢,我就大胆的试了一下将reloadCutVideo方法放在主线程中调用,代码如下:

exportSession.exportAsynchronouslyWithCompletionHandler { () -> Void in
    switch (exportSession.status) {
        case AVAssetExportSessionStatus.Completed:
            print("Export Complete. Status:\(exportSession.status), Error: \(exportSession.error)")
            dispatch_async(dispatch_get_main_queue(), { [weak self] () -> Void in
                self!.reloadCutVideo(self!.videoCacheURL, pointType: pointType)
            })
        case AVAssetExportSessionStatus.Failed:
            print("Failed: \(exportSession.error)")
        default:
            break;
    }
}

OK,成功~,UI的变更,一定要放在主线程中,数据的加载最好是放在子线程。

相关文章

网友评论

  • 也来玩:你好,请问一下,
    exportAsynchronouslyWithCompletionHandler这个方法在iOS8上面有没有崩溃,我这边调用这个方法在iOS8上面会崩溃, malloc: *** error for object 0x170e25d0: pointer being freed was not allocated
    *** set a breakpoint in malloc_error_break to debug,但是在iOS9,没有发送崩溃情况
    也来玩:@AppleDev 显示是指针错误 可就是在那个函数崩溃的,你有没有在iOS8上面试过 保持是ma4格式的
    TomatosX:@夜阑卧风 根据提示来看,你的崩溃应该是哪里的指针被错误的释放了
    TomatosX:@夜阑卧风 我这里无论是在iOS 9 还是在iOS 8 上面都没有发生崩溃的情况,只是在iOS 9的后台有报错信息,但并不崩溃。iOS 8 没有错误信息。

本文标题:更新UI出错的解决方法

本文链接:https://www.haomeiwen.com/subject/wwnrkttx.html