Calling bookmarkData() from a different thread produces error

Hello all,

I am using a AVAggregateAssetDownloadTask to download a stream. In the session's init, the delegateQueue is set to main.

I create and resume session tasks from a separate thread:

func doSomething async {
   Task {
    //Task setup here
    mySessionTask.resume()
   }
}

Now, in the delegate, I have:

   public func urlSession(
        _ session: URLSession,
        aggregateAssetDownloadTask: AVAggregateAssetDownloadTask, 
        willDownloadTo location: URL
    ) {
        
        print("Location: \(location.path)")
        do {
            let data = (try location.bookmarkData())
            print("Great! We generated bookmark data with length \(data.count)")
            
        } catch {
            print("Ooops, an error occurred:  \(error)")
        }

The problem is, when I run the code I get an error:

Error Domain=NSCocoaErrorDomain Code=260 "The file couldn’t be opened because it doesn’t exist."

I am not sure why this is the case. My code is basically exactly the same as a sample from Apple: https://developer.apple.com/documentation/avfoundation/media_playback_and_selection/using_avfoundation_to_play_and_persist_http_live_streams . I can reproduce my error here if I generate the task from a different thread

This leads me to believe, there is a problem (probably with security scoping, but doesn't make sense to me) when trying to access a URL from a different thread in order to generate a bookmark. Has anyone experience this? Any ideas how it can be fixed?

Thanks in advance.

Calling bookmarkData() from a different thread produces error
 
 
Q