Hi everyone, I am DHL. ByteCode, focus on the latest technology to share original articles, involving Kotlin, Jetpack, algorithm animation, data structure, system source code, LeetCode/point Offer/multithreading/domestic and foreign large factory algorithm problems and so on.

The abandonment of the LiveData.Observe () extension is nothing new. A long time ago, when Google abandoned this method, I posted a message to my moments and nuggets boiling point, as shown in the picture below.

In this article you will learn the following:

  • Why the increaseLiveData.observe()Extension method?
  • If it has been added, why should it be scrappedLiveData.observe()Extension method?
  • Kotlin 1.4 syntax features
    • What are functional (SAM) interfaces?
    • What is SAM transformation?

Why deprecate the LiveData.observe extension method

Let’s take a look at the official explanation, as shown below:

Kotlin 1.4 itself has the ability to convert the default Observe () method to lambda syntax, previously only available with the Kotlin extension. Therefore, the livedata.observe () extension method is deprecated.

Livedata.observe () was written as follows before Kotlin 1.4.

liveData.observe(this, Observer<String> {
    // ......
})
Copy the code

But this is a bit complicated to write, so Google has added extensions to the Lifecycle – Livedata-ktx library to make the code cleaner and more readable.

liveData.observe(this){
    // ......
}
Copy the code

In Kotlin 1.4, SAM conversions for Kotlin interfaces were added to support conversion of SAM (single abstract method) interfaces into lambda expressions. Therefore, the livedata.observe () extension method is deprecated. So upgrade the lifecycle- Livedata – KTX library to the latest version and you will get the following prompt.

The migration is also very simple. After upgrading to Kotlin 1.4, all you need to do is remove the import of the following packages.

import androidx.lifecycle.observe
Copy the code

Why add the LiveData.observe extension method

Let’s take a look at the origin of the livedata.observe () extension method from a question posed by a deity in Issuetracker, as shown below:

Livedata.observe () is more complex than Java8.

// java8 liveData.observe(owner, name -> { // ...... }); Livedata. observe(this, Observer<String> {name -> //...... After}) / / SAM conversion liveData. Observe (this) {name - > / /... }Copy the code

Two points of Kotlin syntax need to be inserted here:

  • What are functional (SAM) interfaces?
  • What is SAM transformation?

What are functional (SAM) interfaces

Interfaces that have only one abstract method are called functional interfaces or SAM (Single Abstract method) interfaces. Functional interfaces can have multiple non-abstract members, but only one abstract member.

What is SAM transformation

For functional interfaces, SAM transformations can be implemented with lambda expressions to make the code cleaner and more readable, as shown below.

fun interface ByteCode { fun follow(name: String) } fun testFollow(bytecode: ByteCode) { // ...... TestFollow (object: ByteCode{override fun follow(name: String) {//...... }}) // SAM convert testFollow{//...... }Copy the code

Before Kotlin 1.4, SAM conversion was not supported, so Google added the liveData.observe () extension method to the lifecycle- Livedata-ktx library to achieve the same goal, commit as shown below.

After Kotlin 1.4, Kotlin began supporting SAM conversion, so Google scrapped the liveData.Observe () extension, which was also discussed by Google engineers, as shown in the figure below.

Sergey Vasilinets suggests discarding it in this release in order not to break source code compatibility. The error level will be updated to Error in a later version update, so it is recommended to remove the import of the following packages from the code if you have upgraded to Kotlin 1.4.

import androidx.lifecycle.observe
Copy the code

Compiling with Dynamic Invocations (InvokeDynamic) in Kotlin 1.5.0 to implement SAM(single abstract method) transformations is beyond the scope of this article and will be discussed later. Kotlinlang.org/docs/whatsn…


A “like” would be the biggest encouragement if it helps

More code, more articles

Welcome to the public account: ByteCode, continue to share the latest technology



Finally, recommend long-term update and maintenance projects:

  • Personal blog, will all articles classification, welcome to check hi-dhl.com

  • KtKit compact and practical, written in Kotlin language tool library, welcome to check KtKit

  • Androidx-jetpack-practice androidX-Jetpack-practice androidX-Jetpack-practice androidX-Jetpack-Practice androidX-Jetpack-Practice

  • LeetCode/multiple thread solution, language Java and Kotlin, including a variety of solutions, problem solving ideas, time complexity, spatial complexity analysis

    • Job interview with major companies at home and abroad
    • LeetCode: Read online

Must-read articles these days

  • Follow the source data structure | circular queue
  • Diagram ArrayDeque is faster than LinkedList
  • Why not recommend ArrayDeque instead of Stack
  • | algorithm animation diagrams are “abandoned” Java stack, why still use
  • Kotlin code affecting Performance (1)
  • Jetpack Splashscreen parsing | power generation IT migrant workers get twice the result with half the effort
  • Kotlin’s Technique and Analysis (3)
  • Kotlin’s Technique and Principle Analysis that few people know (II)
  • Kotlin’s Technique and Principle Analysis that few people know (1)
  • Uncover the == and === in Kotlin
  • The Kotlin hermetic class evolved
  • The sealed class in Kotlin is superior to the tagged class
  • What is Kotlin Sealed? Why does Google use it all
  • Android 12 behavior changes that affect applications
  • AndroidStudio
  • Kotlin StateFlow search features practice DB + NetWork
  • The end of the Kotlin plugin and the rise of ViewBinding