最近的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的变更,一定要放在主线程中,数据的加载最好是放在子线程。