最近的iOS項(xiàng)目中,遇到了一個(gè)奇怪的問題(好像是iOS 9上面才有)挪鹏,報(bào)錯(cuò)信息如下:
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.
問題出現(xiàn)在我截取視頻成功之后需要調(diào)用重新載入視頻的方法去更新頁面上的信息,代碼示例如下:
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
方法就是我需要調(diào)用來重新加載視頻的方法愉烙。但是一調(diào)用就會(huì)出現(xiàn)上面的錯(cuò)誤信息讨盒,而且debug發(fā)現(xiàn),reloadCutVideo
方法根本沒有執(zhí)行步责。
我后來試過用通知的方式調(diào)用方法返顺,結(jié)果還是一樣禀苦,最后終于想到,會(huì)不會(huì)是更新頁面不能放在子線程中呢遂鹊,我就大膽的試了一下將reloadCutVideo方法
放在主線程中調(diào)用振乏,代碼如下:
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的變更昆码,一定要放在主線程中,數(shù)據(jù)的加載最好是放在子線程邻储。