是否可以强制实施LiveData值的非空性?

Igo*_*lov 10 android android-livedata android-architecture-components

有没有办法强制实现LiveData值的不可空性?默认的Observer实现似乎有@Nullable注释,它强制IDE建议该值可能为null并且应该手动检查:

public interface Observer<T> {
    /**
     * Called when the data is changed.
     * @param t  The new data
     */
    void onChanged(@Nullable T t);
}
Run Code Online (Sandbox Code Playgroud)

Sir*_*lot 12

如果您使用 ,则可以使用新选项Kotlin。您可以替换LiveDataStateFlow。它更适合Kotlin代码并提供内置的空安全性。

而不是使用:

class MyViewModel {
    val data: LiveData<String> = MutableLiveData(null) // the compiler will allow null here!
}

class MyFragment: Fragment() {
    model.data.observe(viewLifecycleOwner) {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以使用:

class MyViewModel {
    val data: StateFlow<String> = MutableStateFlow(null) // compilation error!
}

class MyFragment: Fragment() {
    lifecycleScope.launch {
        model.data.collect {
          // ...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

StateFlow是协程的一部分,要使用它,lifecycleScope您需要添加lifecycle-extensions依赖项:

implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
Run Code Online (Sandbox Code Playgroud)

请注意,此 API 在协程 1.4.0之前已处于实验状态。

这里有一些关于替换LiveData为 的附加阅读内容StateFlow

正如Igor Bubelov指出的那样,这种方法的另一个优点是它不Android具体,因此可以在多平台项目的共享代码中使用。


Sir*_*lot 6

仅当您控制设置数据的代码时才可以安全地执行此操作,因为您还必须包装该类LiveData。这样,数据设置方法将受到保护,@NonNull并且您可以确保数据在到达之前已经经过检查Observer

封装该类LiveData

public class NonNullMutableLiveData<T> extends MutableLiveData<T> implements NonNullLiveData<T> {

    private final @NonNull T initialValue;

    public NonNullMutableLiveData(@NonNull T initialValue) {
        this.initialValue = initialValue;
    }

    @Override
    public void postValue(@NonNull T value) {
        super.postValue(value);
    }

    @Override
    public void setValue(@NonNull T value) {
        super.setValue(value);
    }

    @NonNull
    @Override
    public T getValue() {
        //the only way value can be null is if the value hasn't been set yet.
        //for the other cases the set and post methods perform nullability checks.
        T value = super.getValue();
        return value != null ? value : initialValue;
    }

    //convenience method
    //call this method if T is a collection and you modify it's content
    public void notifyContentChanged() {
        postValue(getValue());
    }

    public void observe(@NonNull LifecycleOwner owner, @NonNull NonNullObserver<T> observer) {
        super.observe(owner, observer.getObserver());
    }
}
Run Code Online (Sandbox Code Playgroud)

创建一个接口以公开为不可变:

public interface NonNullLiveData<T> {

    @NonNull T getValue();

    void observe(@NonNull LifecycleOwner owner, @NonNull NonNullObserver<T> observer);
}
Run Code Online (Sandbox Code Playgroud)

最后,包裹Observer

//not implementing Observer<T> to make sure this class isn't passed to
//any class other than NonNullMutableLiveData.
public abstract class NonNullObserver<T> {

    public Observer<T> getObserver() {
        return new ActualObserver();
    }

    public abstract void onValueChanged(@NonNull T t);

    private class ActualObserver implements Observer<T> {

        @Override
        public void onChanged(@Nullable T t) {
            //only called through NonNullMutableLiveData so nullability check has already been performed.
            //noinspection ConstantConditions
            onValueChanged(t);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在您可以像这样创建数据:

class DataSource {
    private NonNullMutableLiveData<Integer> data = new NonNullMutableLiveData<>(0);

    public NonNullLiveData<Integer> getData() {
        return data;
    }
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

dataSource.getData().observe(this, new NonNullObserver<Integer>() {
            @Override
            public void onValueChanged(@NonNull Integer integer) {

            }
        });
Run Code Online (Sandbox Code Playgroud)

完全null安全。


Hen*_*Tao 5

如果您使用 Kotlin,您可以使用扩展创建更好的非空观察函数。有一篇关于它的文章。https://medium.com/@henrytao/nonnull-livedata-with-kotlin-extension-26963ffd0333


小智 4

虽然您可以做一些事情,但您有责任确保您不会传递nullLiveData. 除此之外,每个“解决方案”更多的是抑制警告,这可能很危险(如果您确实得到空值,您可能无法处理它,Android Studio 也不会警告您)。

断言

你可以加assert t != null;。该断言不会在 Android 上执行,但 Android Studio 可以理解它。

class PrintObserver implements Observer<Integer> {

    @Override
    public void onChanged(@Nullable Integer integer) {
        assert integer != null;
        Log.d("Example", integer.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

抑制警告

添加注释以抑制警告。

class PrintObserver implements Observer<Integer> {

    @Override
    @SuppressWarnings("ConstantConditions")
    public void onChanged(@Nullable Integer integer) {
        Log.d("Example", integer.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

删除注释

@Nullable这也适用于我安装的 Android Studio,但它可能不适合您,但您可以尝试从实现中删除注释:

class PrintObserver implements Observer<Integer> {

    @Override
    public void onChanged(Integer integer) {
        Log.d("Example", integer.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

默认方法

您不太可能在 Android 上使用它,但纯粹从 Java 角度来看,您可以定义一个新接口并在默认方法中添加 null 检查:

interface NonNullObserver<V> extends Observer<V> {

    @Override
    default void onChanged(@Nullable V v) {
        Objects.requireNonNull(v);
        onNonNullChanged(v);
        // Alternatively, you could add an if check here.
    }

    void onNonNullChanged(@NonNull V value);
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,看起来我们现在唯一能做的就是使用解决方法。感谢您描述最常见的方法! (2认同)