Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WorkManager not working when app is killed #333

Open
Newsid opened this issue Jul 5, 2021 · 16 comments
Open

WorkManager not working when app is killed #333

Newsid opened this issue Jul 5, 2021 · 16 comments
Assignees

Comments

@Newsid
Copy link

Newsid commented Jul 5, 2021

Version Information

  • Visual Studio version: 8.10.4 (build 11)
  • Xamarin.Android version: 11.3.0.4 (Visual Studio Community)
  • Using AndroidX or Support Libraries: Xamarin.AndroidX.Work.Runtime (2.5.0.2)

Describe your Issue:

Im trying to setup a WorkManager that executes a task every 15 minutes. For now I'm doing this test adding an entry in a firebase database.

I'm using the nuget package Xamarin.AndroidX.Work.Runtime (2.5.0.2)

This works fine if the app is in foreground or background, but if I kill the app it stops working.

I'm doing this test with a Nokia 8 Sirocco (api 28) and with a Samsung Galaxy A21S (api 30) that both mount Android Stock.

This is how I launch the work:

private void StartParallelWorkManager(int minutes)
     {
         PeriodicWorkRequest taskLauncher = PeriodicWorkRequest.Builder.From<UniquePeriodicWorker>(TimeSpan.FromMinutes(minutes)).Build();
         WorkManager.GetInstance(Xamarin.Essentials.Platform.AppContext).EnqueueUniquePeriodicWork("TestTask", ExistingPeriodicWorkPolicy.Keep, taskLauncher);
     }

and this is my Worker class:

public class UniquePeriodicWorker : Worker
     {
         public UniquePeriodicWorker(Context context, WorkerParameters workerParameters) : base(context, workerParameters)
         {
         }
    
         public override Result DoWork()
         {
                
             System.Console.WriteLine("{0} DO WORK", DateTime.Now.ToString("dd/MM HH:mm:ss"));
             try
             {
                 _ = CrossCloudFirestore.Current
                              .Instance
                              .Collection("WorkManagerRuns")
                              .AddAsync<DateTime>(DateTime.Now);
             }
             catch(Exception ex)
             {
                 System.Diagnostics.Debug.WriteLine(ex);
             }
             return Result.InvokeSuccess();
         }
     }

Doing the same test in android studio (using Kotlin) works like a charm.

Am I missing something in Xamarin side or Xamarin.AndroidX.Work.Runtime is not capable to achieve the same behavior of the native equivalent??

@Newsid Newsid added the packages label Jul 5, 2021
@AntRemo
Copy link

AntRemo commented Aug 1, 2021

Hi @Newsid did you find a solution?

@Newsid
Copy link
Author

Newsid commented Aug 2, 2021

Hi @AntRemo, not yet. I think it is a problem with this library that is not able to achieve the same behavior of the native equivalent.

@moljac
Copy link
Member

moljac commented Aug 23, 2021

@Newsid

Thanks for the feedback

I think it is a problem with this library that is not able to achieve the same behavior of the native equivalent.

Are you sure? Can you provide minimal repro sample for issue reproduction, please? Thanks

@moljac moljac self-assigned this Aug 23, 2021
@djhango
Copy link

djhango commented Nov 15, 2021

@moljac

I'm encountering this issue as well, see below for attached sample for reproduction:
WorkManager.zip

Version Information:

  • Visual Studio Enterprise 2022 Version 17.0.0
  • Xamarin.AndroidX.Work.Runtime 2.7.0

Comparatively, here's a sample Android native implementation of the above repro:
WorkManagerAndroid.zip

Version Information:

  • Android Studio 2020 3.0.0
  • AndroidX.Work 2.5.0

@Daoting
Copy link

Daoting commented Feb 22, 2022

@moljac

I'm encountering this issue as well.

Version Information:

  • Visual Studio Enterprise 2022 Version 17.2.0 preview1

  • Xamarin.AndroidX.Work.Runtime 2.7.1.2

@moljac
Copy link
Member

moljac commented Feb 22, 2022

@Daoting @djhango Thanks a lot for the feedback. Appreciated.

I will try to sit and dig into the issue and sample (BTW @djhango thanks a lot) ASAP, but my day is 24 hrs which is currently not enough.

@Daoting
Copy link

Daoting commented Aug 24, 2022

@moljac
This bug still exists.

Version Information:
Visual Studio Enterprise 2022 Version 17.3.2
Xamarin.AndroidX.Work.Runtime 2.7.1.4

@temanado
Copy link

temanado commented Sep 8, 2022

This bug still exists

Microsoft Visual Studio Community 2022 Version 17.2.2
Xamarin.AndroidX.Work.Runtime 2.3.4.6

@IgorSava
Copy link

This bug still exists

Microsoft Visual Studio Community 2022 Version 17.4.1
Xamarin.AndroidX.Work.Runtime 2.7.1.5

@kkppstudios
Copy link

kkppstudios commented Dec 12, 2022

But still exists.

Microsoft Visual Studio Community 2022 Version 17.4.2
Xamarin.AndroidX.Work.Runtime 2.7.1.5

Anyone have any updates or work arounds for this??

@buithienquyet
Copy link

buithienquyet commented Dec 26, 2022

Any updates now? It seem to be hard to change because workers call C# code instead of native java code. I think we must to write worker in all java code => create jar => inject back to MAUI

@Tviljan
Copy link

Tviljan commented Mar 16, 2023

Is this bug still relevant? Is someone working on it?

@onurcoskun14
Copy link

This bug still exists

Microsoft Visual Studio Community 2022 Version 17.4.4
Xamarin.AndroidX.Work.Runtime 2.8.1.1

@Zintom
Copy link

Zintom commented Sep 17, 2023

Any updates now? It seem to be hard to change because workers call C# code instead of native java code. I think we must to write worker in all java code => create jar => inject back to MAUI

Damn that's a shame if true. I'm curious why the worker can't launch the C# function, surely we could set something up where Java is called and then that uses JNI to call into the C# runtime.

@pkzOR
Copy link

pkzOR commented Dec 14, 2023

still an issue w/ Xamarin.AndroidX.Work.Runtime v 2.9.0

@pkzOR
Copy link

pkzOR commented Dec 18, 2023

I am rescinding my previous comment after reading this:
https://developer.android.com/topic/performance/background-optimization

The key point here is:
"The precise restrictions imposed are determined by the device manufacturer."

All I had to do was go into Settings | App | myApp | Battery and set it to Unrestriced. Then, it started working. Any other battery setting did not work.

Note: Add logging in DoWork(), connect device to PC, allow USB tethering, build, deploy to device, watch the device log (in VS, Tools | Android | Device Log) to see it working. Run app, kill app, change battery restrictions, etc. You do need to wait the minimum time of 15 minutes.

@jpobst jpobst removed the packages label Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests