๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿค2024 ์•ˆ๋“œ๋กœ์ด๋“œ/๐Ÿฟ ์˜ํ™” ํ”„๋กœ์ ํŠธ ๊ฐœ๋ฐœ ์ผ์ง€

๋ฉ”์ธ ํ™”๋ฉด : ๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ ๊ตฌํ˜„ ๊ณผ์ • ์ •๋ฆฌ

by hyeonha 2024. 5. 10.

๋ชฉ์ฐจ

    ํ™”๋ฉด์ด ์ผœ์งˆ ๋•Œ ๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ”๋กœ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๋Š” ํ˜„์ƒ

    ํ˜„์ƒ :

    - ๋ฉ”์ธ ์•กํ‹ฐ๋น„ํ‹ฐ์˜ ๋ ˆ์ด์•„์›ƒ๋งŒ ๋ณด์ด๊ณ  ํ”„๋ž˜๊ทธ๋จผํŠธ ๋‚ด๋ถ€์˜ ๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ๊ฐ€ ๋ณด์ด์ง€ ์•Š๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜์˜€๋‹ค. 

     

    ์›์ธ

    - ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์— ๋”ฐ๋ผ UI๊ฐ€ ๊ฐฑ์‹ ๋˜์ง€ ์•Š์•„ ๋ฐœ์ƒ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค!

     

    ํ•ด๊ฒฐ

    - ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์ผ์–ด๋‚˜๋Š” ๊ณผ์ •์„ ์ •ํ™•ํžˆ ์ˆ™์ง€ํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. 

    - ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์— ๋”ฐ๋ผ ui ๋ณ€๊ฒฝ์„ ์ฒ˜๋ฆฌํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์—†์„๊นŒ ?

    ๐Ÿซฑ Livedata์™€ observe๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

     

    ๊ธฐ์กด ์ฝ”๋“œ

    val reportList = reportViewModel.report.value
            if (reportList != null) {
                val reviewAdapter = ReviewAdapter()
                reviewAdapter.setReportList(reportList)
                binding.reviewRecyclerView.apply {
                    layoutManager = LinearLayoutManager(context)
                    adapter = reviewAdapter
                }
            } else {
                Log.d("reportList๋Š” null", reportList.toString())
            }

     

     observe๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์ˆ˜์ •ํ•œ ์ฝ”๋“œ

    reportViewModel.report.observe(viewLifecycleOwner) { reportList ->
    if (reportList != null) {
         val reviewAdapter = ReviewAdapter()
          reviewAdapter.setReportList(reportList)
          binding.reviewRecyclerView.apply {
          layoutManager = LinearLayoutManager(context)
           adapter = reviewAdapter
    }

     

    ๊ธฐ์กด ์ฝ”๋“œ์™€ ์ˆ˜์ •ํ•œ ์ฝ”๋“œ ์ฐจ์ด ์•Œ์•„๋ณด๊ธฐ

    ๊ธฐ์กด ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ

    report.value๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ ์‹œ UI๊ฐ€ ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธ๋˜์ง€ ์•Š๊ณ  ๋‹ค๋ฅธ ํƒญ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ui๊ฐ€ ๊ฐฑ์‹ ๋˜๋ฉด์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณด์ธ ๊ฒƒ์ด์—ˆ๋‹ค.

     

    ์ˆ˜์ •ํ•œ ์ฝ”๋“œ

    ๋ทฐ ๋ชจ๋ธ์˜ LiveData ๊ฐ์ฒด์ธ report๋ฅผ ๊ด€์ฐฐํ•œ๋‹ค. ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€์„œ _report.value๊ฐ€ ์—…๋ฐ์ดํŠธ๋˜๋ฉด ๊ด€์ฐฐ์ž์ธ Fragment์—๊ฒŒ ์•Œ๋ ค์ค€๋‹ค. ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด ๋‚ด๋ถ€์— ์ž‘์„ฑํ•ด์ค€ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์ด๋‹ค!

    ๋”ฐ๋ผ์„œ observe๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด report ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ์ด๋ฅผ ๊ด€์ฐฐํ•˜์—ฌ UI๋ฅผ ๋ณ€๊ฒฝํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

     

    Observable์ด ๋ฌด์—‡์ผ๊นŒ??

    LiveData๋Š” ๋ฐ์ดํ„ฐ ํ™€๋” ์—ญํ• ์„ ํ•˜๋ฉฐ ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์ด ๋ฐœ์ƒํ•˜๋ฉด ๊ด€์ฐฐ์ž๋“ค์—๊ฒŒ ์•Œ๋ ค์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ํ•œ๋‹ค.

    observe ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด LiveData์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ์ง€์ •ํ•œ ์ฝœ๋ฐฑ์ด ์‹คํ–‰๋œ๋‹ค. 

     

    observable์ด๋ž€

    ํ•œ ๊ตฌ์„ฑ ์š”์†Œ์™€ ๋‹ค๋ฅธ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ํ†ต์‹ ํ•˜๋Š” ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ ํ•œ ๊ฐ์ฒด์—์„œ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ๋กœ ๊ฐ–๊ณ  ์ง์ ‘ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

    ๊ทธ๋Ÿฌ๋‚˜ ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ๋Š” ์ด ๋ฐฉ๋ฒ•์— ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค.

    ์•ˆ๋“œ๋กœ์ด๋“œ ๊ตฌ์„ฑ ์š”์†Œ๋Š” ์ˆ˜๋ช…์ฃผ๊ธฐ์™€ ์ˆ˜๋ช…์ด ์žˆ๋‹ค. 

    ํ™”๋ฉด ํšŒ์ „ : ํ™œ๋™์„ recreateํ•œ๋‹ค.

     ํ™œ๋™์—์„œ ๋ทฐ๋ชจ๋ธ์„ ์ฐธ์กฐ๋กœ ๊ฐ€์ง€๋Š” ๊ฒƒ์€ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋กœ ์ด์–ด์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ข‹์ง€ ์•Š๋‹ค.

     

     

    ๋”ฐ๋ผ์„œ ๋ทฐ ๋ชจ๋ธ์˜ ํ™œ๋™์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ๊ฐ€์ง€๋Š” ๋Œ€์‹  ํ™œ๋™์˜ ๋ทฐ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ๊ฐ–๋„๋ก ํ•˜์ž.

     

    ๊ทธ๋Ÿฌ๋ฉด ๋ทฐ๋ชจ๋ธ์—์„œ ํ™œ๋™์œผ๋กœ ์–ด๋–ป๊ฒŒ ํ†ต์‹ ํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ผ๊นŒ?

    ํ™œ๋™์ด ๋ทฐ๋ชจ๋ธ์„ ๊ด€์ฐฐํ•˜๋„๋ก ํ•˜์ž!!

     

    ์ด๋ฅผ ์œ„ํ•ด Observable Livedata๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

     

    ์ด๋ ‡๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ViewModel์—์„œ Livedata๋ฅผ ๋…ธ์ถœํ•œ๋‹ค.

          private var _report =                                                                                     
              MutableLiveData<ArrayList<ReportList>>()                                                              
                                                                                                                
          val report: LiveData<ArrayList<ReportList>>                                                               
              get() =   _report

     

    ๊ทธ๋ฆฌ๊ณ  ํ™œ๋™ / ํ”„๋ž˜๊ทธ๋จผํŠธ์—์„œ๋Š” ์‹ค์ œ ๊ตฌ๋…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. Livedata ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋œ๋‹ค. 

     reportViewModel.report.observe(viewLifecycleOwner) { reportList ->
                reviewAdapter = ReviewAdapter()
                reviewAdapter.setReportInfo(reportList)
                initRecyclerView()
            }

     

    ์ด๋ ‡๊ฒŒ ๋ทฐ๋ชจ๋ธ์—์„œ ๋…ธ์ถœํ•ด์ค€ report ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ํ”„๋ž˜๊ทธ๋จผํŠธ์—์„œ๋Š” ์ˆ˜ํ–‰ํ•  ๋™์ž‘์„ ์ •์˜ํ•ด์ค„ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

     

    ์ด๋ ‡๊ฒŒ ๋ฉ”์ธ ํ™”๋ฉด์— ๋ณด์—ฌ์ค„ ์ „์ฒด ๊ฒŒ์‹œ๊ธ€๋“ค์„ ์„œ๋ฒ„์—์„œ ๋ฐ›์•„์™€์„œ ๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ๋ฅผ ํ†ตํ•ด ํ”„๋ž˜๊ทธ๋จผํŠธ์— ๋ณด์—ฌ์ฃผ๋Š” ์ž‘์—…์„ ์™„๋ฃŒํ•˜์˜€๋‹ค.

     

     


    viewLifecyclerOwner์™€ lifecycleOwner์˜ ์ฐจ์ด๋Š”?

    livedata๋ฅผ observeํ•  ๋•Œ viewLifecyclerOwner ์™€ lifecycleOwner์˜ ์ฐจ์ด๊ฐ€ ๋ฌด์—‡์ด๊ณ , ๋‚˜์˜ ๊ฒฝ์šฐ์—๋Š” ๋ฌด์—‡์ด ์ ์ ˆํ•œ์ง€ ์ดํ•ดํ•ด๋ณด์ž

     

    ์šฐ์„  lifecyclerOwner์€ ํ”„๋ž˜๊ทธ๋จผํŠธ ์ž์ฒด์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. onAttach๋ถ€ํ„ฐ onDestory๊นŒ์ง€ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋‹ค.

    fragment view์˜ lifecycler๋ณด๋‹ค fragment lifecycler์ด ๊ธธ๋‹ค.

    ๋งŒ์•ฝ ๋ทฐ๋Š” ์†Œ๋ฉธ๋˜์—ˆ๋Š”๋ฐ fragment ์ž์ฒด๊ฐ€ ์†Œ๋ฉธ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ livedata๋ฅผ observeํ•˜๊ฒŒ ๋˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

    ๋ทฐ๊ฐ€ ์‚ฌ๋ผ์กŒ๊ธฐ ๋•Œ๋ฌธ์— getView๋ฅผ ํ•˜๋ฉด null์ด ๋ฐœ์ƒํ•˜๊ณ  IllegalStateException์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    viewLifecycleOwner์€ ํ”„๋ž˜๊ทธ๋จผํŠธ ๋ทฐ์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

    ์ด ํด๋ž˜์Šค๋Š” onCreateView ~ onDestroyView๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

    ๋”ฐ๋ผ์„œ ํ”„๋ž˜๊ทธ๋จผํŠธ ui๊ฐ€ ํ™œ์„ฑํ™”๋˜์–ด์žˆ๊ฑฐ๋‚˜ ๋น„ํ™œ์„ฑํ™”๋˜์–ด์žˆ์„ ๋•Œ ์—ฐ๊ฒฐ๋œ๋‹ค.

     

    LiveData๋Š”?

    livedata๋Š” observer์˜ lifecycler์„ ๋”ฐ๋ผ๊ฐ„๋‹ค. ์—ฌ๊ธฐ์„œ observer์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์•กํ‹ฐ๋น„ํ‹ฐ๋‚˜ ํ”„๋ž˜๊ทธ๋จผํŠธ์— ํ•ด๋‹นํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋ทฐ๊ฐ€ ์‚ฌ๋ผ์ง€๊ฒŒ ๋˜๋ฉด livedata๋„ ๊ฐ™์ด ์‚ฌ๋ผ์ง„๋‹ค. 

     

    LiveData ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

    MutableLiveData์ธ _data๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

    LiveData์ธ data๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

     

    observe๋ฅผ ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์˜ ์ฐจ์ด

     

    ๋ฐฉ๋ฒ• 1

            reportViewModel.report.observe(viewLifecycleOwner) { reportList ->
                Log.d("์ข‹์•„์š” reportList observe ํ›„", reportList.toString())
                reviewAdapter = ReviewAdapter()
                reviewAdapter.setReportInfo(reportList)
                initRecyclerView()
            }

     

    ๋ฐฉ๋ฒ• 2

           val reportObserver =
                Observer<ArrayList<ReportList>> { reportList ->
                    reviewAdapter = ReviewAdapter()
                    reviewAdapter.setReportInfo(reportList)
                    initRecyclerView()
                }
    
            reportViewModel.report.observe(viewLifecycleOwner, reportObserver)

     

    • LiveData ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•
      • postValue
        • ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์Šค๋ ˆ๋“œ์—์„œ ๋™์ž‘ํ•˜๋‹ค๊ฐ€ ๋ฉ”์ธ ์Šค๋ ˆ๋“œ์— ๊ฐ’์„ postํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.
        • ๋ฉ”์ธ ์Šค๋ ˆ๋“œ ์ ์šฉ ์ „์— postValue()๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ํ˜ธ์ถœ๋œ๋‹ค๋ฉด ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ํ˜ธ์ถœ๋œ ๊ฐ’์ด ์ ์šฉ๋œ๋‹ค.
        • Handler๋ฅผ ํ†ตํ•ด ๋ฉ”์ธ ์Šค๋ ˆ๋“œ์— ๊ฐ’์ด ์ „๋‹ฌ๋˜๊ธฐ ์ „์— getValue๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ๋•Œ๋ฌธ์— postValue ํ˜ธ์ถœ ๋’ค ๋ฐ”๋กœ getValue๋กœ ๊ฐ’์„ ์ฝ์œผ๋ ค๊ณ  ํ•˜๋ฉด ๋ณ€๊ฒฝ๋œ ๊ฐ’์„ ์ฝ์–ด์˜ค์ง€ ๋ชปํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Œ!
        • ๋”ฐ๋ผ์„œ ๋ฐ”๋กœ ๊ฐ’์„ ์ฝ์–ด์•ผํ•  ๋•Œ๋Š” setValue๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
      • setValue
        • ๋ฉ”์ธ ์Šค๋ ˆ๋“œ์—์„œ Livedata์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
        • ๋”ฐ๋ผ์„œ setValue๋ฅผ ํ†ตํ•ด ํ•จ์ˆ˜ ํ˜ธ์ถœ ํ›„ ๋ฐ”๋กœ getValue๋กœ ๊ฐ’์„ ์ฝ์–ด์˜ค๋ฉด ๋ณ€๊ฒฝ๋œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค!
    728x90