https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
n

Nick

10/02/2020, 9:37 PM
Is this any better than having using the backing property that we usually see? This is way has one less property to instantiate so would that make this more memory efficent?
Copy code
protected val titleLiveData = MutableLiveData<String>()
    
    fun getTitleLiveData(): LiveData<String> {
          return titleLivaData
    }
Copy code
this._titleLiveData = new MutableLiveData();
this.titleLiveData = (LiveData)this._titleLiveData;
This is what it compiles down to if there is a backing property, since it’s just copying it over its just as efficent, right?
compiling down to bytecode, it shows that the traditional approach with the backing field is more efficent. Traditional approach with backing field:
Copy code
LINENUMBER 33 L0
    ALOAD 0
    GETFIELD com.blahblahblah.MyViewModell.title : Landroidx/lifecycle/LiveData;
    ARETURN
Approach with the getter function:
Copy code
LINENUMBER 29 L0
    ALOAD 0
    GETFIELD com.blahblahblah.MyViewModell._title : Landroidx/lifecycle/MutableLiveData;
    CHECKCAST androidx/lifecycle/LiveData
    ARETURN
The traditional approach doesn’t have the extra cast in it. Memory is the same it appears.
4 Views