Making a network request with app in background with Siesta and Swift

381 Views Asked by At

I'm making an HTTPS request that's initiated from a UNUserNotification action, so it performs with my app running in the background. I suspect that this feature isn't supported by Siesta, in which case I'll make a feature request! But first I want to see if I'm correct about that.

When I set up & make a request in the background using Siesta, no request appears to be made until I open my app, when I get a URLSessionTask error code -1003 "HTTP load failed" with message: URL Session Task Failed: A server with the specified hostname could not be found..

This is the exact same behavior I see if I use plain URLSession to make requests, without configuring the session for background execution using the boilerplate:

let config = URLSessionConfiguration.background(withIdentifier: String(format: "%f", Date().timeIntervalSinceReferenceDate))

config.isDiscretionary = isDiscretionary
config.requestCachePolicy = .reloadIgnoringLocalCacheData
config.sessionSendsLaunchEvents = true
config.timeoutIntervalForResource = backgroundTimeoutInterval

return URLSession(configuration: config,
                  delegate: self,
                  delegateQueue: .main)

Is there an out-of-the-box way to get this behavior in Siesta?

1

There are 1 best solutions below

3
Paul Cantrell On

Yes, Siesta currently does not officially support background requests, though it doesn’t officially rule them out either.

It's not entirely clear what background requests should mean in Siesta. It’s an in-memory cache, so where does the requested data go? However, Siesta’s persistent caches support will soon graduate from “roll your own” to “out of the box,” and at that point background requests become a lot more compelling.

You should be able to pass your Siesta service constructor a URLSession configured for background requests as in your example above, and Siesta will happily use it to make requests, same as ever. Less clear is what should happen to those responses when they arrive; you’d be on the hook for persisting the response as necessary.