URL Session handling

I'm trying to understand what is the best way to fetch data from the internet and display it in the Widget.

Code Block swift
    func timeline(with context: Context, completion: @escaping (Timeline<SampleEntry>) -> ()) {
        let currentDate = Date()
        let entry = ...
        let timeline = Timeline(entries: [entry], policy: .never)
        let urlSession: URLSession = {
            let config = URLSessionConfiguration.background(withIdentifier: "MySession")
            return URLSession(configuration: config, delegate: URLSessionDel(), delegateQueue: nil)
        }()
        let backgroundTask = urlSession.downloadTask(with: URL(string: "https://example.com")!)
        backgroundTask.resume()
        completion(timeline)
    }


I also added .onBackgroundURLSessionEvents to my WidgetConfiguration. When I run the app I get following error:

BackgroundSession <3A34B7CB-DD73-482F-9F08-8916F11D9624> connection to background transfer daemon invalidated

I watched the session but don't think it was explained well. Additionally I would like to know why in the example from the session there is no occurance of background session? They use mock API and just call completion inside the closure (in func timeline...)
connection to background transfer daemon invalidated is usually an indication of a code signing issue. Are you running this on macOS, iOS, or iOS simulator?
I found a blogpost that has a very simple and understandable example of how data to download data to the widget.

For some reasons I'm not allowed to post the link on this forum though? To find it simply google ios 14 widget kit tutorial and select the top hit. The domain is swiftrocks dot com.
It's iOS app.
This blogpost doesn't explain how to use background tasks which should be used to avoid making requests in timeline func and calling completion after more than 30s.
I'm too struggling to find a way to use correctly network fetching for the widget.

Theres no clear example on Apples Doc, and After implementing the classes and delegates as per Apple's Doc, I receive the same message as you:

Code Block
connection to background transfer daemon invalidated


It would help a lot if the demo code from apple,


BuildingWidgetsUsingWidgetKitAndSwiftUI

Had the networking code.

Do you guys know of an example with the networking code working?


There is unfortunately a known issue with background URL tasks:

onBackgroundURLSessionEvents(matching:_:) might not be called as expected. (64671952)

(From the beta 2 release notes)
URL Session handling
 
 
Q