In my iOS application I am using AVAssetExportSession in order to compress the video file
if let exportSession = AVAssetExportSession(asset: singleVideoComposition, presetName: AVAssetExportPresetMediumQuality) {
exportSession.outputFileType = .mp4
exportSession.timeRange = videoTrack.timeRange
await exportSession.export() // cause a main thread to stall when called for the very first time
if exportSession.status == .completed {
completion(outputURL)
} else {
completion(nil)
}
}
This code is running inside Task. The issue is - on the very first compression it stalls the main thread for 1-2 seconds which causes unpleasant feeling in UI. After first use, it does not stall main thread at all, it works in the background and UI is nice and smooth.
Why is this happening and how to adress that issue? Thank you!
Just my 2 cents.
If you create that
Taskfrom the main actor, then you are running the code on the main actor itself.To check that add this line
Once confirmed that you are on the main actor, let's focus on this
We don't know where the export function runs the operation.
But you can try!
When you create the Task you mentioned, make sure it's not on the main actor using this code.