Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

[Enhancement/RFC] Background tasks abstraction? #181

Closed
knocte opened this issue Aug 2, 2020 · 5 comments
Closed

[Enhancement/RFC] Background tasks abstraction? #181

knocte opened this issue Aug 2, 2020 · 5 comments
Labels
feature-request A request for a new feature. needs-more-discussion This needs more discussion or info before discussing it as a proposal.

Comments

@knocte
Copy link

knocte commented Aug 2, 2020

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).

@knocte knocte added the feature-request A request for a new feature. label Aug 2, 2020
@AndreiMisiukevich AndreiMisiukevich added the needs-more-discussion This needs more discussion or info before discussing it as a proposal. label Aug 2, 2020
@Depechie
Copy link
Contributor

Depechie commented Aug 2, 2020

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.

@knocte
Copy link
Author

knocte commented Aug 3, 2020

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).

@knocte
Copy link
Author

knocte commented Aug 3, 2020

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).

@pictos
Copy link
Contributor

pictos commented Aug 29, 2020

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

@knocte
Copy link
Author

knocte commented Sep 1, 2020

@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: xamarin/Essentials#409 . Closing this then. Thanks.

@knocte knocte closed this as completed Sep 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request A request for a new feature. needs-more-discussion This needs more discussion or info before discussing it as a proposal.
Projects
None yet
Development

No branches or pull requests

4 participants