Android MVVM ํŒจํ„ด ๐Ÿค”

yeonยท2021๋…„ 11์›” 18์ผ
0
post-thumbnail

MVVM ํŒจํ„ด

MVVM ํŒจํ„ด์€ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค์˜ ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๋‹จ์ˆœํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ์—์„œ ๋ฐœํ‘œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Model, View, View Model๋กœ ์ด๋ฃจ์–ด์ง„ ๋””์ž์ธ ํŒจํ„ด์ž…๋‹ˆ๋‹ค.

Why use MVVM?

์ „ํ†ต์ ์ธ UI ๊ฐœ๋ฐœ์—์„œ ๊ฐœ๋ฐœ์ž๋Š” ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ View ํด๋ž˜์Šค์— ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ UI, ๋ฐ์ดํ„ฐ,๋น„์ฆˆ๋‹ˆ์Šค ๋™์ž‘ ์‚ฌ์ด์— ๊ฐ•ํ•œ ์˜์กด์„ฑ์ด ์ƒ๊ธฐ๊ณ  ์œ ์ง€๋ณด์ˆ˜์— ์ข‹์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. Android๋กœ ์ƒ๊ฐํ•œ๋‹ค๋ฉด Activity๋‚˜ Fragment์— ๋ชจ๋“  ์ฝ”๋“œ๊ฐ€ ์ž‘์„ฑ๋˜๋Š” ๊ฒƒ ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ์œ ์ง€๋ณด์ˆ˜๋ฅผ ๊ฐ„๋‹จํžˆ ํ•˜๊ธฐ ์œ„ํ•ด View, Model, Glue Code( = ViewModel) ๊ฐ ์š”์†Œ์˜ ๊ด€์‹ฌ์‚ฌ๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” MVVM์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

What is MVVM?

  • Model

์•ฑ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•˜๊ณ , ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

  • View

์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค.

  • View Model

ViewModel์€ Model๊ณผ View์˜ ์ธํ„ฐํŽ˜์ด์Šค ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ๊ณผ ์ปค๋งจ๋“œ ํŒจํ„ด์ด ์ด์šฉ๋˜์–ด View์™€์˜ ์˜์กด์„ฑ์ด ์—†์Šต๋‹ˆ๋‹ค.

๐Ÿ’ก MVVM ํŒจํ„ด์„ ์ด์šฉํ•˜์—ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” MVVM ํŒจํ„ด์˜ ๋ชฉ์ ์ธ ๊ด€์‹ฌ์‚ฌ ๋ถ„๋ฆฌ๋ฅผ ํ•˜๊ธฐ์œ„ํ•ด View์™€ Model, View์™€ View Model์€ ์„œ๋กœ ์˜์กดํ•˜์ง€ ์•Š๋„๋ก ์„ค๊ณ„๋˜์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

Android MVVM ์ ์šฉ ๋ฐฉ๋ฒ•

Android ๊ถŒ์žฅ Architecture

Android์—์„œ๋Š” ๊ด€์‹ฌ์‚ฌ ๋ถ„๋ฆฌ์™€ ๋ชจ๋ธ์—์„œ UI๋ฅผ ๋„์ถœํ•˜๋Š” ๊ฒƒ์„ ์ฃผ์š” ์›์น™์œผ๋กœ ๋งํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ๊ด€์‹ฌ์‚ฌ๋ฅผ ๋ถ„๋ฆฌ : Activity, Fragment๋Š” Android OS๋กœ ์ธํ•ด ์–ธ์ œ๋“ ์ง€ destoryed ๋  ์ˆ˜
  2. ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•ฑ์—์„œ๋Š” Activity, Fragment์— ์ตœ์†Œํ•œ์œผ๋กœ ์˜์กดํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
  3. ๋ชจ๋ธ์—์„œ UI ๋„์ถœ : Android OS์˜ ์˜ํ–ฅ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์žƒ์ง€ ์•Š๊ณ , ๋„คํŠธ์›Œํฌ์˜ ์˜ํ–ฅ์—๋„ ์•ฑ์ด ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•˜๋ ค๋ฉด ์•ฑ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ชจ๋ธ์—์„œ UI๊ฐ€ ๋„์ถœ๋˜์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

Android MVVM ์ ์šฉ ๋ฐฉ๋ฒ•

Android์—์„œ View, Model, View Model์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑํ•˜๋ฉด Android App Architecture Guide์— ๋งž๊ฒŒ MVVM ํŒจํ„ด ์ ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

  • View

Activity, Fragment, xml์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. AAC LiveDta๋ฅผ ํ™œ์šฉํ•˜์—ฌ ViewModel์— ์ž‘์„ฑ๋˜์–ด์ง„ ๋ผ์ด๋ธŒ ๋ฐ์ดํ„ฐ๊ฐ์ฒด๋ฅผ ๊ตฌ๋…ํ•˜๊ณ  ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์ƒํƒœ์— ๋”ฐ๋ผ ์‚ฌ์šฉ์ž์—๊ฒŒ ํ™”๋ฉด์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

  • Model

์ผ๋ฐ˜์ ์œผ๋กœ ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” Room๊ณผ ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋Š” Http Client ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.

  • ViewModel

์ผ๋ฐ˜์ ์œผ๋กœ ํ™”๋ฉด ํšŒ์ „์‹œ ๋ฉ”๋ชจ๋ฆฌ์ƒ ์œ ์ง€๋˜๊ณ , ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ณ ๋ คํ•˜์—ฌ UI ๊ด€๋ จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” AAC ViewModel์„ ์ด์šฉํ•˜์—ฌ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. AAC Data Binding์„ ์‚ฌ์šฉํ•˜์—ฌ MVVM ํŒจํ„ด์—์„œ ์˜๋ฏธํ•˜๋Š” ViewModel์ฒ˜๋Ÿผ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ก AAC ViewModel ์ด์šฉ์‹œ ViewModel์˜ onCleared() ํ•จ์ˆ˜๋ฅผ ์žฌ์ •์˜ํ•˜์—ฌ ํ•ด๋‹น ํ•จ์ˆ˜ ์‹คํ–‰์‹œ ์ง„ํ–‰์ค‘์ด๋˜ ๋น„๋™๊ธฐ ์ž‘์—…๋“ค์ด release ๋˜๋„๋ก ์ฝ”๋“œ๊ฐ€ ์ž‘์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
profile
Android Developer

0๊ฐœ์˜ ๋Œ“๊ธ€