{"id":45671,"date":"2021-11-17T14:13:17","date_gmt":"2021-11-17T05:13:17","guid":{"rendered":"https:\/\/www.charlezz.com\/?p=45671"},"modified":"2021-11-17T14:13:19","modified_gmt":"2021-11-17T05:13:19","slug":"using-state-in-jetpack-compose-viewmodel-%eb%82%b4-state-%ed%85%8c%ec%8a%a4%ed%8a%b8%ed%95%98%ea%b8%b0","status":"publish","type":"post","link":"https:\/\/charlezz.com\/?p=45671","title":{"rendered":"Using state in Jetpack Compose &#8211; ViewModel \ub0b4 state \ud14c\uc2a4\ud2b8\ud558\uae30"},"content":{"rendered":"\n<p>\uc560\ud50c\ub9ac\ucf00\uc774\uc158 \ub85c\uc9c1\uc774 \uc62c\ubc14\ub978\uc9c0 \ud655\uc778\ud558\uae30 \uc704\ud574 ViewModel\uc744 \ud14c\uc2a4\ud2b8\ud558\ub294 \uac83\uc740 \uc88b\uc740 \uc0dd\uac01\uc774\ub2e4. \uc774 \uc139\uc158\uc5d0\uc11c\ub294 \uc0c1\ud0dc\uc5d0 \ub300\ud574 State&lt;T>\ub97c \uc0ac\uc6a9\ud558\uc5ec ViewModel\uc744 \ud14c\uc2a4\ud2b8 \ud558\ub294 \ubc29\ubc95\uc744 \ubcf4\uc5ec\uc8fc\ub294 \ud14c\uc2a4\ud2b8 \ucf54\ub4dc\ub97c \uc791\uc131\ud560 \uac83\uc774\ub2e4.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">TodoViewModelTest\uc5d0 \ud14c\uc2a4\ud2b8 \ucd94\uac00\ud558\uae30<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class TodoViewModelTest {<br><br>&nbsp; &nbsp;@Test<br>&nbsp; &nbsp;fun whenRemovingItem_updatesList() {<br>&nbsp; &nbsp; &nbsp; &nbsp;\/\/ before<br>&nbsp; &nbsp; &nbsp; &nbsp;val viewModel = TodoViewModel()<br>&nbsp; &nbsp; &nbsp; &nbsp;val item1 = generateRandomTodoItem()<br>&nbsp; &nbsp; &nbsp; &nbsp;val item2 = generateRandomTodoItem()<br>&nbsp; &nbsp; &nbsp; &nbsp;viewModel.addItem(item1)<br>&nbsp; &nbsp; &nbsp; &nbsp;viewModel.addItem(item2)<br><br>&nbsp; &nbsp; &nbsp; &nbsp;\/\/ during<br>&nbsp; &nbsp; &nbsp; &nbsp;viewModel.removeItem(item1)<br><br>&nbsp; &nbsp; &nbsp; &nbsp;\/\/ after<br>&nbsp; &nbsp; &nbsp; &nbsp;assertThat(viewModel.todoItems).isEqualTo(listOf(item2))<br>&nbsp; &nbsp;}<br>}<\/code><\/pre>\n\n\n\n<p>\uc774 \ud14c\uc2a4\ud2b8\ub294 \uc774\ubca4\ud2b8\uc5d0 \uc758\ud574 \uc9c1\uc811 \uc218\uc815\ub418\ub294 State&lt;T>\ub97c \ud14c\uc2a4\ud2b8\ud558\ub294 \ubc29\ubc95\uc744 \ubcf4\uc5ec\uc900\ub2e4. \uc774\uc804 \uc139\uc158\uc5d0\uc11c \uc0c8 ViewModel\uc744 \ub9cc\ub4e0 \ub2e4\uc74c todoItems\uc5d0 \ub450 \uac1c\uc758 \ud56d\ubaa9\uc744 \ucd94\uac00\ud55c\ub2e4.<\/p>\n\n\n\n<p>\uc6b0\ub9ac\uac00 \ud14c\uc2a4\ud2b8\ud560 \uba54\uc18c\ub4dc\ub294 \ubaa9\ub85d\uc758 \uccab \ubc88\uc9f8 \uc544\uc774\ud15c\uc744 \uc81c\uac70\ud558\ub294 removeItem\uc774\ub2e4.<\/p>\n\n\n\n<p>\ub9c8\uc9c0\ub9c9\uc73c\ub85c \ubaa9\ub85d\uc5d0 \ub450 \ubc88\uc9f8 \uc544\uc774\ud15c\ub9cc \ud3ec\ud568\ub418\uc5b4 \uc788\ub2e4\uace0 \ud655\uc2e0\ud558\uae30 \uc704\ud574 assert \uba54\uc11c\ub4dc\ub97c \uc0ac\uc6a9\ud55c\ub2e4.<\/p>\n\n\n\n<p>\uc5c5\ub370\uc774\ud2b8\uac00 \ud14c\uc2a4\ud2b8\uc5d0 \uc758\ud574 \uc9c1\uc811 \ubc1c\uc0dd\ud55c \uacbd\uc6b0(\uc5ec\uae30\uc5d0\uc11c removeItem\uc744 \ud638\ucd9c\ud558\uc5ec \uc218\ud589\ud558\ub294 \uac83\ucc98\ub7fc) \ud14c\uc2a4\ud2b8\uc5d0\uc11c todoItems\ub97c \uc77d\uae30 \uc704\ud574 \ucd94\uac00 \uc791\uc5c5\uc744 \uc218\ud589\ud560 \ud544\uc694\uac00 \uc5c6\ub2e4. \uc774\uac74 \uadf8\ub0e5 List&lt;TodoItem>\uc77c \ubfd0\uc774\ub2e4.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><strong>\uacbd\uace0:<\/strong> MutableState&lt;T>\uc5d0 \ub300\ud55c \uc4f0\uae30\uac00 \ub2e4\ub978 \uc2a4\ub808\ub4dc\uc5d0\uc11c \uc218\ud589\ub418\uba74 \ud14c\uc2a4\ud2b8\uc5d0\uc11c \uc989\uc2dc \ud45c\uc2dc\ub418\uc9c0 \uc54a\ub294\ub2e4. <\/p><p>\ubcc0\uacbd \uc0ac\ud56d\uc744 \ud45c\uc2dc\ud558\uae30 \uc704\ud55c \uc800\uc218\uc900 API\ub294 Snapshot.sendApplyNotifications()\uc774\ub2e4.<\/p><p>\uc774\ub97c \ucc98\ub9ac\ud558\uae30 \uc704\ud55c \ub192\uc740 \uc218\uc900\uc758 API\ub294 \ud604\uc7ac \uc791\uc5c5 \uc911\uc774\uba70, \uc791\uc5c5\uc774 \uc644\ub8cc\ub418\uba74 \uc774 <a href=\"https:\/\/developer.android.com\/codelabs\/jetpack-compose-state?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fcompose%3Fhl%3Dko%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fjetpack-compose-state#9\">\ucf54\ub4dc\ub7a9<\/a>\uc740 \uc5c5\ub370\uc774\ud2b8 \ub41c\ub2e4.<\/p><\/blockquote>\n\n\n\n<p>\uc774 ViewModel\uc5d0 \ub300\ud55c \ub098\uba38\uc9c0 \ud14c\uc2a4\ud2b8\ub294 \ub3d9\uc77c\ud55c \uae30\ubcf8 \ud328\ud134\uc744 \ub530\ub974\ubbc0\ub85c \uc774 \ucf54\ub4dc\ub7a9 \uc5f0\uc2b5\uc6a9\uc73c\ub85c \ub450\uace0 \uac74\ub108\ub6f0\ub3c4\ub85d \ud55c\ub2e4. ViewModel\uc758 \ub354 \ub9ce\uc740 \ud14c\uc2a4\ud2b8\ub97c \ucd94\uac00\ud558\uc5ec \uc791\ub3d9\ud558\ub294\uc9c0 \ud655\uc778\ud558\uac70\ub098, \uc644\uc131\ub41c \ubaa8\ub4c8\uc5d0\uc11c TodoViewModelTest\ub97c \uc5f4\uc5b4 \ub354 \ub9ce\uc740 \ud14c\uc2a4\ud2b8\ub97c \ubcfc \uc218 \uc788\ub2e4.<\/p>\n\n\n\n<p>\ub2e4\uc74c \uc139\uc158\uc5d0\uc11c\ub294 \uc0c8\ub85c\uc6b4 \ud3b8\uc9d1\ubaa8\ub4dc\ub97c UI\uc5d0 \ucd94\uac00\ud558\ub3c4\ub85d \ud558\uc790.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><strong>\uacbd\uace0:<\/strong> \ud14c\uc2a4\ud2b8\uc5d0\uc11c State&lt;T> \uac1d\uccb4\uc5d0 \ub300\ud55c \uc4f0\uae30\ub97c \uae30\ub2e4\ub9ac\ub294 API\uac00 \ud604\uc7ac \uc5c6\ub2e4.<\/p><p>\uc608\ub97c \ub4e4\uc5b4, \ub370\uc774\ud130\ubca0\uc774\uc2a4 \ud638\ucd9c\uc5d0 \uc758\ud574 todoItem\ub4e4\uc774 \ube44\ub3d9\uae30\uc801\uc73c\ub85c \ucc44\uc6cc\uc9c4 \uacbd\uc6b0, \uc5c5\ub370\uc774\ud2b8\ub420 \ub54c\uae4c\uc9c0 \uae30\ub2e4\ub9b4 \ubc29\ubc95\uc774 \ud604\uc7ac\ub85c\uc11c\ub294 \uc5c6\ub2e4. 1.0 \uc774\uc804\uc5d0 \uc774\uc5d0 \ub300\ud55c API\uac00 \uc788\uc744 \uac83\uc73c\ub85c \uc608\uc0c1\ud558\uc9c0\ub9cc \uc544\uc9c1 \uc900\ube44\ub418\uc9c0 \uc54a\uc558\ub2e4.<\/p><\/blockquote>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uc560\ud50c\ub9ac\ucf00\uc774\uc158 \ub85c\uc9c1\uc774 \uc62c\ubc14\ub978\uc9c0 \ud655\uc778\ud558\uae30 \uc704\ud574 ViewModel\uc744 \ud14c\uc2a4\ud2b8\ud558\ub294 \uac83\uc740 \uc88b\uc740 \uc0dd\uac01\uc774\ub2e4. \uc774 \uc139\uc158\uc5d0\uc11c\ub294 \uc0c1\ud0dc\uc5d0 \ub300\ud574 State&lt;T>\ub97c \uc0ac\uc6a9\ud558\uc5ec ViewModel\uc744 \ud14c\uc2a4\ud2b8 \ud558\ub294 \ubc29\ubc95\uc744 \ubcf4\uc5ec\uc8fc\ub294 \ud14c\uc2a4\ud2b8 \ucf54\ub4dc\ub97c \uc791\uc131\ud560 \uac83\uc774\ub2e4. TodoViewModelTest\uc5d0 \ud14c\uc2a4\ud2b8 \ucd94\uac00\ud558\uae30 \uc774 \ud14c\uc2a4\ud2b8\ub294 \uc774\ubca4\ud2b8\uc5d0 \uc758\ud574 \uc9c1\uc811 \uc218\uc815\ub418\ub294 State&lt;T>\ub97c \ud14c\uc2a4\ud2b8\ud558\ub294 \ubc29\ubc95\uc744 \ubcf4\uc5ec\uc900\ub2e4. \uc774\uc804 \uc139\uc158\uc5d0\uc11c \uc0c8 ViewModel\uc744 \ub9cc\ub4e0 \ub2e4\uc74c todoItems\uc5d0 \ub450 \uac1c\uc758 \ud56d\ubaa9\uc744 \ucd94\uac00\ud55c\ub2e4. \uc6b0\ub9ac\uac00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[38],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts\/45671"}],"collection":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=45671"}],"version-history":[{"count":1,"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts\/45671\/revisions"}],"predecessor-version":[{"id":45672,"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts\/45671\/revisions\/45672"}],"wp:attachment":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=45671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=45671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=45671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}