Xamarincommunitytoolkit: [Enhancement/RFC] Background tasks abstraction?

Created on 2 Aug 2020  路  5Comments  路  Source: xamarin/XamarinCommunityToolkit

Summary

Is there any interest in bringing a background-tasks solution to the CommunityToolkit that abstracts the API of many platforms into one? Doing this with Xamarin.Forms these days is quite a hassle because you need to touch a lot of platform-specific code, e.g. https://xamarinhelp.com/xamarin-background-tasks/

However I've put together a project myself that abstracts all these details into one common API: https://github.com/nblockchain/XamarinFormsBackground (disclaimer: as you can see at the top, my repo is actually a fork from another author; the main difference is it doesn't bring extraneous dependencies such as Reactive Extensions).

If this is a good idea, I can prepare a PR to integrate this into the CommunityToolkit.

API Changes

A background task would then be created like this:

public class SomeBackgroundWork : IBackgroundTask
{
    public SomeBackgroundWork()
    {
    }

    public Task StartJob()
    {
        // YOUR CODE HERE

        return;
    }
}

And it would need to be started this way:

protected override void OnStart()
{
    //Register Background Tasks
    BackgroundAggregatorService.Add(() => new SomeBackgroundWork());
    BackgroundAggregatorService.Add(() => new OtherBackgroundWork());

    //Start the background service
    BackgroundAggregatorService.StartBackgroundService();
}

Each platform would require a bit of initialization routines (but this initialization code would not need to change everytime more tasks are added or the current tasks are modified, unlike the current state of affairs described in the xamarinhelp.com article above).

feature-request needs-more-discussion

Most helpful comment

Doesn't Shiny https://github.com/shinyorg/shiny provide this functionality? If so, I think my opinion would be to not add the same functionality in the toolkit for the moment.

All 5 comments

Doesn't Shiny https://github.com/shinyorg/shiny provide this functionality? If so, I think my opinion would be to not add the same functionality in the toolkit for the moment.

Hey thanks for the info, I didn't know about Shiny.

TBH, if Shiny's API is deemed to be better than my Xamarin.Forms.Background lib, I'm fine with that. I opened this github issue to check if it's desirable to include backgrounding features into this XamarinCommunityToolkit, and if we end up with something more similar to Shiny, that's ok too.

That said, IMO integrating Shiny's approach would have 2 downsides (compared to Xamarin.Forms.Background):

a) It's a lib that includes many more features, so separating the backgrounding bits might be tricky. This is the list of features that Shiny docs highlight:

  • A Centralized Hosting Platform
  • Environment (App & Device Information)
  • Logging
  • Connectivity
  • File System & IO Extensions
  • Power Management
  • Permissions
  • Settings
  • Background Jobs
  • Geofencing
  • GPS (Foreground & Background)
  • Bluetooth LE (GATT Peripheral & Central)
  • Beacons
  • Speech Recognition
  • HTTP File Transfers
  • Sensors
  • Notifications
  • Integrations with 3rd Party MVVM Frameworks like Prism, ReactiveUI, & MvvmCross

b) Shiny embraces some dependencies which maybe are not desirable to have in XamarinCommunityToolkit, such as Reactive Extensions (RX) and Microsoft Extension (DI).

Forgot a 3rd downside: Shiny doesn't seem to support some platforms that Xamarin.Forms.Background supports: macOS, Tizen & Gtk (as they only mention iOS, Android, and UWP in their docs).

@knocte, for me, this feature fits better in Xamarin.Essentials.

@pictos agreed, and I also asked in #essentials channel in DotNetEvolution discord and a maintainer also agrees, and later I have found the github issue there: https://github.com/xamarin/Essentials/issues/409 . Closing this then. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hnabbasi picture hnabbasi  路  6Comments

rdelrosario picture rdelrosario  路  3Comments

jfversluis picture jfversluis  路  5Comments

jfversluis picture jfversluis  路  5Comments

varyamereon picture varyamereon  路  4Comments