Submitting data when backgrounded

JonasRembrattJonasRembratt SEUniversity ✭✭✭

I have a weird problem in our current project. As the app sends a larger chunk of data to the server, to protect it when app gets backgrounded, we're starting a long-running task (using the UIApplication.SharedApplication.BeginBackgroundTask / UIApplication.SharedApplication.EndBackgroundTask API). What's weird is that this works great when I build and run from my machine but several of my colleagues get a timeout error when running the exact same scenario when the app was build/deployed from their machines.

As far as I understand it running stuff in a long-running task like this should work. I should not have to specify backgrounding capabilities in info.plist. Also, as the HttpClient employ NSUrlSession for sending/receiving it should be protected from interruptions when the app gets backgrounded, right?

I can't figure out why the same code yields different behavior on the same device when built with different Macs. Could there be some setting somewhere in VS that could be local to the machine that would affect this behavior?

I'm out of ideas now so any hints would be greatly appreciated.

This is an example of code that works/fails depending on the Mac that built/deployed it:

public async Task Submit()
{
    // ... some irrelevant code
    BackgroundTask.Run(async () => await submitAsync()); // Form-level encapsulation of the UIApplication.SharedApplication.BeginBackgroundTask API
    // ... more irrelevant code
}

private async Task submitAsync()
{
    if (!IsSubmitAllowed)
        return;

    IsBusy = true;
    IsGoBackAllowed = IsGoNextAllowed = false;
    var statusIndicator = new StatusIndicator();
    await PopupNavigation.Instance.PushAsync(statusIndicator);
    try
    {
        statusIndicator.Status = "Saving TI";
        statusIndicator.Progress = 0.1;

        var submitted = await _service.SubmitAsync(Model); // ERROR! causes timeout exception for some
        var submittedId = submitted.BackendId;
    // ... etc.

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Based on my research, it cannot be achieved, beginBackgroundTaskWithExpirationHandler cannot grant unlimited background activity.

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited July 9

    @JonasRembratt

    Are there any updates for this issue? If the reply is helpful, please try to mark it as an answer, it will help others who have similar issue.

  • JonasRembrattJonasRembratt SEUniversity ✭✭✭

    Sorry for the silence on my part. From what I've seen in the documentation a long-running task is indeed not unlimited. Before iOS 7 it used to be a time limit of ten minutes and after iOS 7 less (I haven't seen an exact figure though). What baffles me is that running the code that performs the server roundtrip in a LRT works depending on what machine I build the project. It seems non-deterministic.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @JonasRembratt said:
    What baffles me is that running the code that performs the server roundtrip in a LRT works depending on what machine I build the project. It seems non-deterministic.

    Can you share the link about this conclusion? I cannot find some related information about it.

Sign In or Register to comment.