Xamarin Messaging Center, Long Running Task cancelled unexpectedly, xcode 11.2.1

TomGalvinTomGalvin IEMember ✭✭

I have a Xamarin Forms project and it has a task that runs for about 1-3 mins based on MessagingCenter and especially an article by Rob Gibbens This worked perfectly on iOS and still does on Android (Thankyou Rob) but after compiling using XCode 11.2.1 the task is cancelled as soon as the user moves to a different app on the phone or the phone goes to standby.
Specific iOS code is
` public async Task Start ()
{
_cts = new CancellationTokenSource ();

_taskId = UIApplication.SharedApplication.BeginBackgroundTask ("LongRunningTask", OnExpiration);

try {
    //INVOKE THE SHARED CODE
    var counter = new TaskCounter();
    await counter.RunCounter(_cts.Token);

} catch (OperationCanceledException) {
} finally {
    if (_cts.IsCancellationRequested) {
        var message = new CancelledMessage();
        Device.BeginInvokeOnMainThread (
            () => MessagingCenter.Send(message, "CancelledMessage")
        );
    }
}

UIApplication.SharedApplication.EndBackgroundTask (_taskId);

}`

I suspect this is a change made by Apple in XCode 11.2.1 but not sure how to resolve this. I have tried adding uibackgroundmodes in info.plist but no success. I would really appreciate any pointers. Thankyou.

Best Answer

  • TomGalvinTomGalvin IE ✭✭
    Accepted Answer

    Thanks ColeX. It looks like this is the new approach. I hope I can tie it in with a user triggered task. Once I have code up and running I will post to the forum. Thankyou.

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited November 29

    As the documentation mentioned , do not use this method simply to keep your app running after it moves to the background.

    Perhaps Apple does some changes on BackgroundTask , but before use it , there is something to watch out for:

    • You still have to write the code to create and manage that work

    • You must end every background task that you begin.

    • //xxxx

    Refer https://medium.com/swlh/handling-background-tasks-in-ios-13-67f717d94b3d

  • TomGalvinTomGalvin IEMember ✭✭

    Thankyou for the reply. I have that code to End the task etc present. The only thing that has changed on the project is that this time XCode 11.2.1 was used to compile. Now as soon as a user moves to another app on the phone a cancellation token/event is raised immediately, and handled on my side by cancelling the task. I hope that makes sense.

  • TomGalvinTomGalvin IEMember ✭✭

    I was able to narrow this problem down. The issue is specifically to do with an external Web call. Beginning with XCode 11 is there a new approach to handling a long running task that also interfaces with a Web API.
    A Backgroundtask containing an external request is cancelled after 2-3 seconds.
    Thankyou.

  • ColeXColeX Member, Xamarin Team Xamurai

    Beginning with XCode 11 is there a new approach to handling a long running task that also interfaces with a Web API.

    Apple released a great framework this year,it is the BackgroundTasks framework , maybe you could try it .

    Refer https://medium.com/snowdog-labs/managing-background-tasks-with-new-task-scheduler-in-ios-13-aaabdac0d95b .

  • TomGalvinTomGalvin IEMember ✭✭
    Accepted Answer

    Thanks ColeX. It looks like this is the new approach. I hope I can tie it in with a user triggered task. Once I have code up and running I will post to the forum. Thankyou.

Sign In or Register to comment.