강의

멘토링

커뮤니티

Inflearn Community Q&A

choidaehwan92822002's profile image
choidaehwan92822002

asked

[Intermediate] Friendly Introduction to JETPACK <Part 1> (Android Kotlin)

Map / SwitchMap - 2

Map / SwitchMap - 2 강의 오류

Written on

·

378

·

Edited

0

Map / SwitchMap - 2 강의의 1분 29초 쯤에

val mapLiveData = Transformations.map(liveCount) {
    it+it
}

란 코드가 있는데요 Transformations 를 못불러오고

스크린샷 2023-12-11 오후 1.53.25.png강의자료를 그대로 복붙했는데 이런식으로 빨간줄이 납니다.
import를 못해오는거 같습니다

switchMap도 마찬가지로 안됩니다. Transformations 를 임포트 못받아옵니다!

 

찾아보니 gradle에 이부분을 임포트 받고

implementation("androidx.lifecycle:lifecycle-livedata-core:2.3.1")

이런식으로 사용하니까 되더라고요. 이런식으로 하고 진행해도 되는거겠죠?

class MainViewModel : ViewModel() {

    private var _mutableCount = MutableLiveData(0)
    val liveCount : LiveData<Int>
        get() = _mutableCount

    val mapLiveData = liveCount.map {
        it+it
    }

    val switchMapLiveData = liveCount.switchMap {
        changeValue(it)
    }

    fun setLiveDataValue(count : Int) {
        _mutableCount.value = count
    }

    fun changeValue(count : Int) : MutableLiveData<Int> {
        val testLiveData = MutableLiveData(count*count)
        return testLiveData
    }
}
androidkotlinjetpack

Answer 1

0

bokchi님의 프로필 이미지
bokchi
Instructor

넵 정확한 것은 전체 프로젝트 코드를 봐야 알 것 같지만

말씀하신 것 처럼 사용하셔도 좋습니다!

choidaehwan92822002's profile image
choidaehwan92822002

asked

Ask a question