ios – Creating AVCaptureVideoPreviewLayer object – Publishing adjustments from inside view updates is just not allowed, it will trigger undefined habits


I am making an attempt to open the digicam with AVKit and whereas creating CameraView and AVCaptureVideoPreviewLayer object I’ve the next error:
Publishing adjustments from inside view updates is just not allowed, it will trigger undefined habits.

I do know that it might be resolved with including DispatchQueue.essential.async {} however I have no idea precisely the place to place it.

I am creating CameraView like:

CameraView(dimension: geometry.dimension)
                            .environmentObject(cameraViewModel)
                            .ignoresSafeArea()

Inside, it appear like:

struct CameraView: UIViewRepresentable {
    @EnvironmentObject var cameraViewModel: CameraViewModel
    
    var dimension: CGSize
    
    func makeUIView(context: Context) -> UIView {
        let view = UIView()
        
        /* error happens right here */
        cameraViewModel.capturePreview = AVCaptureVideoPreviewLayer(session: cameraViewModel.captureSession)

        cameraViewModel.capturePreview.body.dimension = dimension
        
        cameraViewModel.capturePreview.videoGravity = .resizeAspectFill
        
        view.layer.addSublayer(cameraViewModel.capturePreview)
        
        return view
    }
    
    func updateUIView(_ uiView: UIView, context: Context) {
        
    }
}

And lastly, CameraViewModel holds capturePreview which is:

@Revealed var capturePreview: AVCaptureVideoPreviewLayer!

Leave a Reply