Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Missing generic Android.Arch.Lifecycle.IObserver<> #215

Open
IngweLand opened this issue Oct 4, 2019 · 2 comments
Open

Missing generic Android.Arch.Lifecycle.IObserver<> #215

IngweLand opened this issue Oct 4, 2019 · 2 comments
Assignees
Labels
investigating Currently investigating issue

Comments

@IngweLand
Copy link

Xamarin.Android Version (eg: 6.0):

10.0

Operating System & Version (eg: Mac OSX 10.11):

Win 10

Support Libraries Version (eg: 23.3.0):

28.0.0.3
Xamarin.Android.Arch.Work.Runtime 1.0.0.0

Describe your Issue:

The progress and the result of Worker can be retrieved by observing the WorkInfo.
https://developer.android.com/topic/libraries/architecture/workmanager/advanced#params

The Java code sample

WorkManager.getInstance(myContext).getWorkInfoByIdLiveData(myWorkRequest.getId())
    .observe(lifecycleOwner, info -> {
                    // ... do something with the result ...
         }
    });

C# code which will not compile

WorkManager.Instance.GetWorkInfoByIdLiveData(myWorkRequest.Id)
                .Observe(lifecycleOwner, info => {
                    // ... do something with the result ...
         });

However, same code cannot be recreated in Xamarin, because generic version of IObserver<> is missing.The non-generic version of IObserver is present in Android.Arch.Lifecycle namespace.

Workaround:

using Android.Arch.Lifecycle;
using AndroidX.Work;
using Java.Lang;

public class MyWorkManager : Object, IObserver
    {
        public void OnChanged(Object p0)
        {
            var wi = p0 as WorkInfo;
            if (wi == null)
            {
                return;
            }

             // ... do something with the result ...
        }

        public void StartWork()
        {
             OneTimeWorkRequest myRequest =
                OneTimeWorkRequest.Builder.From<MyWorker>().Build();

            WorkManager.Instance.Enqueue(myRequest);
            WorkManager.Instance.GetWorkInfoByIdLiveData(myRequest.Id)
                .Observe(lifecycleOwner, this);
        }
    }

Steps to Reproduce (with link to sample solution if possible):

Include any relevant Exception Stack traces, build logs, adb logs:

@moljac moljac self-assigned this Oct 14, 2019
@moljac moljac added the investigating Currently investigating issue label Oct 14, 2019
@JFenlon
Copy link

JFenlon commented Jan 12, 2020

Hello, any updates on this?

@JFenlon
Copy link

JFenlon commented Jan 13, 2020

FYI, found a workaround the null WorkInfo issue for the time being:

public void OnChanged(Java.Lang.Object p0)
{
	var p0List = p0 as IEnumerable;
	
	foreach (var item in p0List )
	{
		var workInfo = item as WorkInfo;

		if (workInfo != null && workInfo.GetState() == WorkInfo.State.Succeeded)
		{
                     // Handle Success state here
		}
	}
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
investigating Currently investigating issue
Projects
None yet
Development

No branches or pull requests

3 participants