Background Tasks for Uploading Data? Xamarin Forms - Android / iOS

VUhlmann 1 Reputation point
2021-05-20T23:28:16.533+00:00

Hey,
I'm currently trying to figure out how to use a dependency service to create an upload task for data that will still run after closing the app / minimizing / moving it to the background. I need to upload large amounts of data to a server and I'm not sure if I need a foreground or background service on Android and how the iOS equivalent would look like. The examples I found are mostly tasks that should be executed every X minutes and not as a one-time task.

I hope someone can help! Many greetings

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Kyle Wang 5,531 Reputation points Microsoft External Staff
    2021-05-21T07:02:57.45+00:00

    Hi Uhlmann,

    Welcome to our Microsoft Q&A platform!

    For Android, to upload data in the background, you can try to create Android service.

    A service is an application component which can perform long-running operations in the background. It does not provide a user interface. After startup, even if the user switches to another application, the service will continue to run for a while. About Android service, here is a Xamarin.Android documentation: Creating Android Services.

    For iOS, you can call NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration(String) Method.

    NSUrlSessionConfiguration backgroundSessionConfiguration = NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration(@"identifier");  
    NSUrlSession backgroundSession = NSUrlSession.FromConfiguration(backgroundSessionConfiguration);  
    NSUrlSessionUploadTask uploadtask = backgroundSession.CreateUploadTask(request, fileURL);  
    

    It will create a session configuration object that allows HTTP and HTTPS uploads or downloads to be performed in the background. The session with "background session configuration" will run in a separate process. In iOS, data is still transferred when the application is suspended or terminated. Here is a “Background transfer” documentation you can refer to: Background transfer and NSURLSession in Xamarin.iOS.

    Best Regards,
    Kyle Wang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.