xamarin / AndroidSupportComponents

Xamarin bindings for Android Support libraries - For AndroidX see https://github.com/xamarin/AndroidX
MIT License
146 stars 56 forks source link

WorkManager's methods return type is different from Android's documentation. #169

Closed xyfoo closed 5 years ago

xyfoo commented 5 years ago

Package

Xamarin.Android.Arch.Work.Runtime 1.0.0

Issue

AndroidX.Work.WorkManager.GetWorkInfosByTagLiveData method only return LiveData instead of LiveData<List<WorkInfo>, as stated in the AndroidX's documentation for the method.

image

Current method's metadata as displayed in Visual Studio 2017.

[Register("getWorkInfosByTagLiveData", "(Ljava/lang/String;)Landroid/arch/lifecycle/LiveData;", "GetGetWorkInfosByTagLiveData_Ljava_lang_String_Handler")]
public abstract LiveData GetWorkInfosByTagLiveData(string p0);

By not having a typed LiveData object, we would not be able to retrieve the value of the LiveData as below:

LiveData myLiveData = WorkManager.Instance.GetWorkInfosByTagLiveData("myTag");

// The following will fail as LiveData.Value will return a Java.Lang.Object item
// and the conversion will be unsuccessful.

List<WorkInfo> workInfos = (List<WorkInfo>)myLiveData.Value;

WorkManager methods with the same issue

Version

moljac commented 5 years ago

In C# method/function signature does not include return type.

Have you ever tried to "overload" method based on return type? This is no-no (nyet-nyet) in C#. These are some language idiomatic diffs Binders (people that bind libraries) face. So, pasting links for AndroidX docs is nice, but will not guarantee that workaround will be found.

So, C# bindings return base class LiveData as a LCD (Least Common Denominator). I agree I could return Java.Lang.Object, but you would hate me even more.

Please, check if you can find workaround.

Closing this one soon.

Playing: https://www.youtube.com/watch?v=jM8dCGIm6yc

Redth commented 5 years ago

Try using .JavaCast<T> in this case:

var workInfos = myLiveData.Value.JavaCast<List<WorkInfo>>();
yelinzh commented 4 years ago

Try using .JavaCast<T> in this case:

var workInfos = myLiveData.Value.JavaCast<List<WorkInfo>>();

Hi! The code caused the following exception. How to deal with it?

The type 'System.Collections.Generic.IList<AndroidX.Work.WorkInfo>' cannot be used as type parameter 'TResult' in the generic type or method 'Extensions.JavaCast<TResult>(IJavaObject)'. There is no implicit reference conversion from 'System.Collections.Generic.IList<AndroidX.Work.WorkInfo>' to 'Android.Runtime.IJavaObject'.