How can I perform cross platform background HttpClient Post for Xamarin?

Viewed 512

I would like to implement a cross platform Upload library for my Xamarin project that can perform background upload to a Rest Api.

I am implementing the iOS client at the moment, and I have selected the HttpClient implementation to be NSUrlSession in Project Settings.

My Upload library implements standard HttpClient methods for syncing. For example (stripped down for simplicity) ...

public static async Task<HttpResponseMessage> Upload(string url, HttpContent content) {
    return await client.PostAsync(url, content);
}

Now I would like to implement a background Upload when the user presses a "Finish Button" in the UI. Xamarin offers three backgrounding techniques iOS Backgrounding with Tasks

I'm trying to decide whether to use Background-Safe Tasks or Background Transfers (iOS 7+), and I was hoping to clarify the following:

  • If I choose Background Transfers, am I correct in stating that I'm now tying my Upload Manager to a platform specific implementation, since the Background Transfer is an NSUrlSession feature, and although HttpClient implements NSUrlSession it doesn't implement this type of specific functionality.

  • If I choose Background-Safe Tasks, I am spinning up a background task and I'm still using NSUrlSession, but my Upload Manager implementation can be cross platform since it's using the HttpClient in a more generic way and not using Background Transfers.

Thanks for the feedback.

0 Answers
Related