Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ble-livedata allows non-nullable MutableLiveData to be null in kotlin #493

Open
MaiHeu opened this issue Apr 17, 2023 · 2 comments
Open

Comments

@MaiHeu
Copy link

MaiHeu commented Apr 17, 2023

I have the following piece of code:

private val _connectedPeripheral: MutableLiveData<MyDevice> = MutableLiveData()
public val connectedPeripheral: LiveData<MyDevice>
    get() = _connectedPeripheral
private val bleManager = MyBleManager(context)

public fun disconnect() {
    if (bleManager.currentConnectedDevice == null)
        return
    bleManager.disconnect()
    _connectedPeripheral.value = null //this should not be allowed, as the LiveData wasn't declared nullable
    startScanning()
}

It compiles and runs, though it would add the possibility of a NPE where it isn't expected.
With the current androidx.lifecycle:lifecycle-livedata-core-ktx:2.6.1 package it rightfully gets marked by Android Studio as error and thus does not compile.

@philips77
Copy link
Member

philips77 commented Apr 17, 2023

Hello, are you're suggesting updating dependencies?
The library module already depends on 2.6.1:

api 'androidx.lifecycle:lifecycle-livedata:2.6.1'

@MaiHeu
Copy link
Author

MaiHeu commented Apr 17, 2023

I rather thought it's due to an implementation error, but digging further into it it seems the issue is a java <-> kotlin one.
I can reproduce said problem when exchanging the ble-livedata with androidx.lifecycle:lifecycle-livedata:2.6.1

Internally, the package androidx.lifecycle:lifecycle-livedata-core-ktx seems to ensure and enforce the null safety.

This issue would probably fit with #483 for a kotlin specific package :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants