child 的 LiveData 与 parent 的 LiveData 不兼容的类型

Incompatible types with LiveData of a child as a LiveData of a parent

我想使用 MutableLiveData 观察来自 ViewModel 的一些数据。问题是我用的是child和parentclass,和LiveData有些不兼容。 我想在 Kotlin 中做的一个例子:

import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel

class Test : ViewModel() {

    abstract class Parent(protected var id: Int)

    class ChildFirst(id: Int) : Parent(id)

    class ChildSecond(id: Int) : Parent(id)

    var childFirst : MutableLiveData<ChildFirst> = MutableLiveData<ChildFirst>()

    var childSecond : MutableLiveData<ChildSecond> = MutableLiveData<ChildSecond>()

    var shouldManageFirstChild = true

    fun returnCorrectChild(): MutableLiveData<Parent> {
        if (shouldManageFirstChild) {
            return childFirst //won't compile in Android Studio (Type mismatch)
        } else {
            return childSecond as MutableLiveData<Parent> //compile and work with a warning in AndroidStudio (Unchecked cast)
        }
    }
}

这里是 Java :

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

public class Test extends ViewModel {
    class Parent {
        protected int mId;

        Parent(int id) {
            mId = id;
        }
    }

    class ChildFirst extends Parent {
        ChildFirst(int id) {
            super(id);
        }
    }

    class ChildSecond extends Parent {
        ChildSecond(int id) {
            super(id);
        }
    }

    MutableLiveData<ChildFirst> childFirst = new MutableLiveData <ChildFirst>();

    MutableLiveData<ChildSecond> childSecond = new MutableLiveData <ChildSecond>();
    boolean shouldManageFirstChild = true;

    MutableLiveData<Parent> returnCorrectChild(){
        if (shouldManageFirstChild) {
            return childFirst; //won't compile in Android Studio (Incompatible type)
        } else {
            return (MutableLiveData<Parent>) childSecond;   //won't compile in Android Studio (Inconvertible cast)
        }
    }
}

如您所见,问题是编译器认为 MutableLiveData 和 MutableLiveData 的类型不同。

在 Kotlin 中,我可以将 child 的 LiveData 转换为 parent。即使有警告,代码 运行 也符合预期:我可以观察到 MutableLiveData.

更糟糕的是,在 Java 中,即使使用强制转换也无法编译。

所以这是我的问题:

您可以将 returnCorrectChild() 函数重构为 return 通用类型,然后检查 returned 类型是否是 ViewModel 外部的 ChildFirst 或 ChildSecond 实例class

类似于:

  fun returnCorrectChild(): MutableLiveData<*> {
        if (shouldManageFirstChild) {
            return childFirst
        } else {
            return childSecond
        }
    }

在你的 viewModel 之外 class:

yourLiveDataInstance.observe(this@YourActivity, Observer {
            when(it){
                is ChildFirst -> {
                    // do stuff for ChildFirst
                }
                is ChildSecond ->{
                    // do stuff for ChildSecond
                }
            }
        })

Why I can't use the LiveData of a child as the LiveData of a parent ? Is that something intended by the LiveData ?

在 java 中答案很简单:Java 中的泛型类型是不变的,这意味着 List<String> 不是 List<Object> 的子类型(引自 kotlin 文档here)

Are they some eventual consequences of casting it with the kotlin 'as' ?

Kotlin 只是在编译时发出警告,但在运行时发生的情况是您的检查仅针对非泛型部分。这就是为什么,我认为(如果有人比我更了解,请解释一下!我对此很感兴趣),你可以在 Kotlin 中做到这一点。

为了更好地解释在使用泛型类型时 kotlin(以及 Java)中发生的情况,我建议您阅读关于泛型的完整 kotlin 文档 here 甚至还有很多关于 java 泛型转换的文章,例如 this one

编辑: 解决您的问题的方法可能是这样的: 声明单个实时数据并以如下简单方式处理 childs:

val liveParent = MutableLiveData<Parent>()
val childFirst = ChildFirst()
val childSecond = ChildSecond()

然后 return 调用 returnCorrectChild()

liveParent 中的正确 child
fun returnCorrectChild() {
    if (shouldManageFirstChild) {
        liveParent.value = firstChild
    } else {
        liveParent.value = secondChild
    }
}