question

Uhlmann avatar image
0 Votes"
Uhlmann asked RobCaplan edited

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

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

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered KyleWang-MSFT edited

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.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Uhlmann Is this answer helpful for you?

0 Votes 0 ·