Hello Friends, while building your mobile app, I’m sure you’ve already come across a situation where you need your app to do some time-consuming work in the background, without interfering with the user’s experience. Such tasks might include synchronizing data, sorting data, unzipping large files, etc. Though C# gives us a way to run asynchronous tasks already, it might not be enough. You could also attempt to do multi-threading, but Android and iOS have built-in mechanisms to properly manage background tasks in Xamarin Forms.

Android’s WorkManager and Workers

Workers are classes responsible for background tasks management in Android. The workers were introduced to offer a flexible and easily configurable way of implementing and scheduling background tasks in Android. Workers perform the tasks given to them in the background, so you shouldn’t worry about it interfering with your UX.

NOTE: As stated in the Android doc, Simple Workers are built to do their tasks in an asynchronous fashion in the background. But their DoWork() method is synchronous. Meaning that you are expected to run the code in it in a synchronous fashion, and finish it when the method exits.

Taking the above statement into consideration, we need a means of running our work in the background in a completely Asynchronous fashion instead. You must have noticed that in C#, almost every method call that talks to APIs, or do some intensive work is an async task. So, using a simple worker won’t be the ideal solution. For this reason, we will cover only ListenableWorkers, in this article.

Building our ListenableWorker

We will build a fake synchronization worker, and simulate a long-running task in it, just for demo purposes. HERE is the source code for this article.

First, install this (Xamarin.AndroidX.Work.Runtime) nugget package to your Android project.

Implement the worker that will perform the synchronization task. To do this, we will create a class that inherits from (ListenableWorker, CallbackToFutureAdapter.IResolver) in the android project.

Then, inside the “AttachCompleter” method, run our long-running synchronization.

Now that we have our synchronization worker completed, we can run this with the work manager as seen below. That will be the second and final part of our implementation of background tasks in Xamarin forms.

Troubleshooting Android Background Workers

You might face a situation where your worker stops working, or it never starts working. And fails silently. There is something tricky about this work manager API that the Android docs do not mention (last time I checked). You should be careful while applying work policies. This article here details what you need to know to resolve issues you might come across.

Background-Safe Tasks on iOS

On iOS’s side, things are a lot easier. If you want to run background tasks anywhere in you application as your app is in the foreground, you’ll need to use what is called “Background-Safe Tasks“. Xamarin iOS provides us a means of easily registering our tasks to run in the background.

When processing finishes, we kill the background task as follows.

Background tasks are given about 10 minutes of execution time by iOS. So, you should listen to expiration and kill the task when the allowed execution time expires.

NOTE: As stated in the documentation, the tasks run with this approach could be in a background thread on the main thread as you wish. You should always end the background task with the task ID given to you, and implement expiration handlers. This will prevent iOS from killing your app.

Here is the source code for this article.

In our case, we’ll create a task, and call directly the synchronization task. All that on a background thread. That will be the second and final part of our implementation of background tasks in Xamarin forms.

Conclusion

Running synchronization, unzipping, or any other long-running tasks in your apps in a proper way is important if you want the UX of your app to be good. I hope this post helps you, and please share and follow me on social media if you find this helpful. You might also be interested in this article about implementing firebase dynamic links in Xamarin Forms.

References

Using WorkManager and ListenableWorker in Xamarin Android

Image Credit ( https://www.yippeecode.com/topics/background-task-in-python-celery/ )

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction?WT.mc_id=DT-MVP-5003277

Follow me on social media and stay updated

2 comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.