How to dynamically update an existing AVComposition when users add a new custom video clip?

I’m building a macOS video editor that uses AVComposition and AVVideoComposition. Initially, my renderer creates a composition with some default video/audio tracks:

@Published var composition: AVComposition?
@Published var videoComposition: AVVideoComposition?
@Published var playerItem: AVPlayerItem?

Then I call a buildComposition() function that inserts all the default video segments.

Later in the editing workflow, the user may choose to add their own custom video clip. For this I have a function like:

private func handlePickedVideo(_ url: URL) {
            guard url.startAccessingSecurityScopedResource() else {
                print("Failed to access security-scoped resource")
                return
            }

            let asset = AVURLAsset(url: url)
            let videoTracks = asset.tracks(withMediaType: .video)

            guard let firstVideoTrack = videoTracks.first else {
                print("No video track found")
                url.stopAccessingSecurityScopedResource()
                return
            }

            renderer.insertUserVideoTrack(from: asset, track: firstVideoTrack)

            url.stopAccessingSecurityScopedResource()
        }

What I want to achieve is the same behavior professional video editors provide, after the composition has already been initialized and built, the user should be able to add a new video track and the composition should update live, meaning the preview player should immediately reflect the changes without rebuilding everything from scratch manually.

How can I structure my AVComposition / AVMutableComposition and my rendering pipeline so that adding a new clip later updates the existing composition in real time (similar to Final Cut/Adobe Premiere), instead of needing to rebuild everything from zero?

You can find a playable version of this entire setup at :- https://github.com/zaidbren/SimpleEditor

How to dynamically update an existing AVComposition when users add a new custom video clip?
 
 
Q