Similar to this this question,
I am having issues and my app is crashing in the same way. I would assume the same answer as on the other question: memory issue; except I am getting the crash during an AVAssetExportSession call.
guard let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality) else { return }
exporter.outputFileType = AVFileTypeMPEG4
exporter.outputURL = url
exporter.videoComposition = mainComposition
print("done")
exporter.exportAsynchronously(completionHandler: {
DispatchQueue.main.async(execute: {
self.exportDidFinish(exporter)
print("removing AI")
self.removeAI()
print("removed AI")
completion()
})
})
func exportDidFinish(_ exporter:AVAssetExportSession) {
if(exporter.status == AVAssetExportSessionStatus.completed) {
print("cool")
}
else if(exporter.status == AVAssetExportSessionStatus.failed) {
print(exporter.error as Any)
}
}
It prints "done" but it never prints "removing AI". It also doesn't print "cool" or "(error)"; it crashes and says at the top of XCode "Lost connection to iPhone..." just as the other question states.
I would assume it is a memory issue, but there is nothing happening in between (to my knowledge of how this works) during the asynchronous exporting as I am just waiting for the completion handler to be called. But nothing gets called in between and I am unsure how to handle this. Any thoughts?
I think that AVAssetExportSession
object may be deallocated while running async task. Consider put it as a class variable to make sure that async block can finish it's task. It will be something like:
class myClass {
var exporter: AVAssetExportSession?
func export() {
exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
guard exporter != nil else { return }
exporter.outputFileType = AVFileTypeMPEG4
exporter.outputURL = url
exporter.videoComposition = mainComposition
print("done")
exporter?.exportAsynchronously(completionHandler: {
DispatchQueue.main.async {
self.exportDidFinish(exporter)
print("removing AI")
self.removeAI()
print("removed AI")
completion()
}
}
}
}
I'm really not sure why you also put everything on a main thread in completion block, but maybe you want to do something with UI later, so I leave it there.
But most import is - make your AVAssetExportSession
not stored on method that may dealloc it. It's a memory issue that could cause this.
I had this issue before, it wasn't a memory issue for me. I don't know how exactly I got resolved, but I these are the things I did:
I deleted the app, hard reset the phone, cleaned build on Xcode, restarted Xcode, deleted derived data, and if I remember correctly changed the USB port until it got fixed.
I also had console and a VM open, I closed them as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With