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

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

Open
@IngweLand

Description

@IngweLand

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:

Metadata

Metadata

Assignees

Labels

investigatingCurrently investigating issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions