작성
·
397
·
수정됨
0
왼쪽이 제가 작성한 부분이고 오른쪽이 선생님강의입니다.
코인 선택화면 API 데이터 받아와서 확인 (Retrofit / Coroutine) 7분에서
1. 제가 작성한 부분이 ViewModel 은 lifecycle이고 선생님께서 작성하신viewModel은 selectViewModel로 되어있는데요.
이거 뭔차이가있나요?
SelectViewModel 클래스를 ViewModel()타입으로 선언한거를 SelectActivity.kt에 ViewModel 변수에 넣어준거 맞나요?
2.코인 선택화면 API 데이터 받아와서 확인 (Retrofit / Coroutine) 10분에서
class NetworkRepository {
private val client=RetrofitInstance.getInstance().create(Api::class.java)
suspend fun getCurrentCoinList1()=client.getCurrentCoinList()//API주소
}
이런코드가있는데요.
getCurrentCoinList1()가 함수인거 같은데 대입연산자 사용이 가능한가요?
3 NetworkRepository에서요.
코드
.class NetworkRepository {
private val client=RetrofitInstance.getInstance().create(Api::class.java)
//클래스RetrofitInstance 에서 retrofit형식을 가진 client를 불러온다.
suspend fun getCurrentCoinList1()=client.getCurrentCoinList()//API주소
}
이런코드가있는데요.
suspend fun getCurrentCoinList1()=client.getCurrentCoinList()//API주소
질문3번 :getCurrentCoinList()함수가 api클래스 내부 함수인거 같은데요.
근데 RetrofitInstance.getInstance()여기서는 api파일을 받은게 없는데 어떻게 api 함수인 getCurrentCoinList()를 쓸수있나요?
}
4. 데이터 코인 선택 Acitivity로 받아오기 (LiveData) 1분22초에서
원래코드는 이건데요.
class SelectViewModel : ViewModel() {
private val netWorkRepository = NetWorkRepository()
private lateinit var currentPriceResultList : ArrayList<CurrentPriceResult>
// 데이터변화를 관찰 LiveData
private val _currentPriceResult = MutableLiveData<List<CurrentPriceResult>>()
val currentPriceResult : LiveData<List<CurrentPriceResult>>
get() = _currentPriceResult
fun getCurrentCoinList() = viewModelScope.launch {
val result = netWorkRepository.getCurrentCoinList()
currentPriceResultList = ArrayList()
for(coin in result.data) {
================================================
class SelectViewModel : ViewModel() {
private val netWorkRepository = NetWorkRepository()
private lateinit var currentPriceResultList : ArrayList<CurrentPriceResult>
fun getCurrentCoinList() = viewModelScope.launch {
// 데이터변화를 관찰 LiveData
private val _currentPriceResult = MutableLiveData<List<CurrentPriceResult>>()
val currentPriceResult : LiveData<List<CurrentPriceResult>>
get() = _currentPriceResult
val result = netWorkRepository.getCurrentCoinList()
currentPriceResultList = ArrayList()
for(coin in result.data) {
이렇게 작성하면 LiveData를 viewModelScope.launch {안에 넣어주면 위사진처럼 get()을 import하면 뭔가 많이 나오는데 이건 왜이런건가요?
답변 1
0
잘 이해하신 것 같은데 ViewModel -> viewModel 로 대소문자 변경해보시겠어요?
아래 이미지처럼 궁금하시면 직접 해보시면 됩니다
4. 전체 코드와 스크린샷을 공유해주시면 좀 더 답변드리기 좋을 것 같습니다~