The Code A is fom the project .
I think Code B is the same as Code A, right?
Code A
val task: LiveData<Task?> = _task
val completed: LiveData<Boolean> = _task.map { input: Task? ->
input?.isCompleted ?: false
}
Code B
val task: LiveData<Task?> = _task
val completed = Transformations.map(_task){input: Task? ->
input?.isCompleted ?: false
}
Yes, it is absolutely identical because LiveData.map
is an extension function that provided from Transformations.kt
file that is a part of dependency:
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
This is what map
extension function does which is absolutely identical to Code B:
inline fun <X, Y> LiveData<X>.map(crossinline transform: (X) -> Y): LiveData<Y> =
Transformations.map(this) { transform(it) }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.