Unreliable Network Requests with Interactive Widgets in Production Environment

We've noticed that network requests triggered when a user interacts with our widget are very unreliable. In development, they almost always work, but in production, many of them never reach the server, especially when many requests are performed in a row.

The requests are performed using the URLSession.shared singleton instance:

let session = URLSession.shared

let task = session.dataTask(with: request) { data, response, error in
    ...
}

task.resume()

Are there any limitations or restrictions on Interactive Widgets in a production environment that might cause this behavior?

Is there a different way to implement or configure widgets to ensure network requests are reliably performed?

Thank you.

Is this code running in your WidgetKit extension? If so…

Well, first a caveat. I have no direct experience with WidgetKit extensions, so all of the following is based on my experience with other app extension types.

I suspect you’ll find that the extension is being suspended, which is causing the network request to fail. How you proceed here depends on the nature of your network requests:

  • For small requests where latency is important, use ProcessInfo to prevent your process from suspending while the request is in flight. See UIApplication Background Task Notes.

    Note that you can’t use a UIApplication background task because an app extension doesn’t have access to UIApplication.

  • For long-running requests, use a background session. This can get rather complex; see Networking in a Short-Lived Extension.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Unreliable Network Requests with Interactive Widgets in Production Environment
 
 
Q