Schedule App Refresh in the background

Hi,
When my app is in the background, I need to wake it up periodically to refresh data. I am not sure how to achieve that since, from what I have seen so far, the URLSessionConfiguration that is passed to the URLSession is .default (in URLSessionClient).
When doing app refreshes in the background, Apple recommends to use a background session configuration.
My confusion arises since the URLSessionClient also has some delegate callbacks for background events, but curiously does not conform to URLSessionDownloadDelegate.
Can someone please advise?

Thanks!

Hi @rajivjhoomuck. This is a more advanced use case, and we don’t currently have easy built-in support for this. Drop-in support for a background session is something we should probably add in the future.

For now, you can provide your own URLSessionConfiguration by creating a custom RequestChain.

As for the URLSessionDownloadDelegate, I’m not clear what would be needed there to support background fetching. The ApolloClient doesn’t provide any methods for doing file downloads. If you create a background session configuration, you would still just do a fetch which would then write the resulting data into the NormalizedCache of you ApolloStore.

Thanks for the reply @AnthonyMDev , it is a little trickier than just providing the background session since (from what I understood and saw till now) in order to schedule fetches when the app is in the background, we should promote the dataTask to a download task and read the downloaded data from the URLSessionDownloadDelegate callback method.

The route that I am want to take is the following:

  • Subclass URLSessionClient
  • Conform it to URLSessionDownloadDelegate in order to handle the downloaded data

But this comes with its own issue: URLSessionClient.tasks is a private property and is not accessible to subclasses, therefore it cannot be mutated. There are no methods to add URLRequest related behaviors (completions, received data, and responses).
For regular requests / fetches we were using the convenient NetworkFetchInterceptor but it seems that I will have to basically the same things because ~URLSessionClientis not open fortasks`.

Please let me know if I am again missing something here.

Thanks

Thanks for the information! Yes, unfortunately, this seems like it’s going to be a bit more difficult for you than I would have hoped. The current APIs here don’t give you the tools to configure this.

You’re probably going to need to make your own new CustomNetworkFetchInterceptor with its own custom URLSessionClient. You can pretty much just copy & past our session client and then add the things you need to it.

This is not how we want this to work. And this feedback is super valuable. Thanks for sharing. The good news is, I’m currently working on re-writing a lot of our networking layer for a 2.0 version of apollo-ios, and I will work on making this easier to configure in 2.0!

Thanks @AnthonyMDev . Yeah I got things working by only subclassing URLSessionClient. The project is using async/await so had to use a local actor to track the completions with the task identifiers which was fun :star_struck:.

If you want more feedback, it will be a pleasure to discuss.

Regards

1 Like