AvAssetWriter fails due to busy mp4 encoder. How to check if AvAssetWriter has finished writing the last frame?

My colleague and I are currently programming an iPhone-App (for iPhones having two cameras) using swift 4.1 for transmitting video and depth data over the network. But we ran into a problem:


We use 2 AvAssetWriter[1]to store Video and Depth (transformed into a Gray-Image) onto the phone. Therefore we use dataOutputSynchronizer[2]and write the video and depth frames with different AvAssetWriters, but one after the other in the same thread.


After some (seemingly random) time the following code


if avAssetWriterInput.isReadyForMoreMediaData {

let successful = avAssetWriterInput.append(sampleBuffer)

}


fails with the following avAssetWriter.errorlocalizedDescription:


**The operation could not be completed**


I suspect the one AvAssetWriter hasn’t finished writing the previous frame and therefore the other AvAssetWriter fails appending the next frame due to the occupied mp4 encoder.


Is there a way I can check if an AvAssetWriter has finished writing and released the encoder, so that another AvAssetWriter can use it? (Or another way to work around this problem)


[1] https://developer.apple.com/documentation/avfoundation/avassetwriter

[2] https://developer.apple.com/documentation/avfoundation/avcapturedataoutputsynchronizerdelegate/2873976-dataoutputsynchronizer