{"id":1830,"date":"2020-04-18T06:23:00","date_gmt":"2020-04-17T21:23:00","guid":{"rendered":"https:\/\/www.itchefblog.com\/?p=1830"},"modified":"2021-04-22T03:03:40","modified_gmt":"2021-04-21T18:03:40","slug":"realm-%ec%82%ac%ec%9a%a9%ed%95%98%ea%b8%b0","status":"publish","type":"post","link":"https:\/\/www.itchefblog.com\/?p=1830","title":{"rendered":"Realm \uc0ac\uc6a9\ud558\uae30"},"content":{"rendered":"\n<p>\ud504\ub85c\uc81d\ud2b8 \uc218\uc900\uc758 gradle\uc758 dependencies\uc5d0 \ub2e4\uc74c\uacfc \uac19\uc774 \ucd94\uac00<\/p>\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndependencies {\n\tclasspath &quot;io.realm:realm-gradle-plugin:5.2.0&quot;\n}\n<\/pre>\n\n\n<p>\ubaa8\ub4c8 \uc218\uc900\uc758 gradle \uc0c1\ub2e8\uc5d0 \ub2e4\uc74c \ucf54\ub4dc \ucd94\uac00<\/p>\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\napply plugin: &quot;kotlin-kapt&quot; \/\/\uc774 \ub77c\uc778\uc774 \uc544\ub798\ub85c \ub0b4\ub824\uac00\uba74 apply plugin: &quot;kotlin-kapt&quot;\n                            \/\/\uc774\ub530\uc704 \uc5d0\ub7ec\uac00 \ubc1c\uc0dd\ud560 \uc218 \uc788\uc74c\napply plugin: &quot;realm-android&quot;\n<\/pre>\n\n\n<p>Todo \ubaa8\ub378 \ud074\ub798\uc2a4 \uc791\uc131<\/p>\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/\ucf54\ud2c0\ub9b0\uc5d0\uc11c\ub294 Realm\uc5d0\uc11c \uc0ac\uc6a9\ud558\ub294 \ud074\ub798\uc2a4\uc5d0 open \ud0a4\uc6cc\ub4dc\ub97c \ucd94\uac00\ud55c\ub2e4.\n\/\/@PrimaryKey\ub294 \uace0\uc720\ud558\uba70, Realm\uc5d0\uc11c \uc81c\uacf5\ud558\ub294 \uc8fc\uc11d\uc774\ub2e4.\nopen class Todo(\n\t\t@PrimaryKey var id: Long = 0,\n\t\tvar title: String = &quot;&quot;,\n\t\tvar date: Long = 0\n) : RealmObject() {\n\n}\n<\/pre>\n\n\n<p>\uc0dd\uc874\ucf54\ub529 399, 400\ud398\uc774\uc9c0 \ucc38\uc870<\/p>\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/\uc0c8 \uac1d\uccb4 \uc0dd\uc131\nvar realm = Realm.getDefaultInstance()\n\nprivate fun insertTodo() {\n    realm.beginTransaction()\n\n    val newItem = realm.createObject&lt;Todo&gt;(nextId())\n\n    newItem.title = editText.text.toString()\n    newItem.date = calendar.timeInMillis\n\n    realm.commitTransaction()\n\n    alert(&quot;\ub0b4\uc6a9\uc774 \ucd94\uac00\ub418\uc5c8\uc2b5\ub2c8\ub2e4.&quot;) {\n        yesButton { finish() }\n    }.show()\n}\n\n\/\/Realm\uc5d0\uc11c\ub294 key\ub97c \uc790\ub3d9\uc99d\uac00 \ud574\uc8fc\ub294 \uae30\ub2a5\uc774 \uc5c6\uae30\uc5d0 \uc218\ub3d9\uc73c\ub85c nextId\ub97c \uac00\uc838\uc640\uc57c\ud568\nprivate fun nextId(): Int {\n    val maxId = realm.where&lt;Todo&gt;().max(&quot;id&quot;)\n    if(maxId != null){\n        return maxId.toInt() + 1\n    }\n    return 0\n}\n<\/pre>\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/\ubaa8\ub4e0 \uac12\uc744 \uc5bb\uc73c\ub824\uba74 where&lt;Todo&gt;() \uba54\uc11c\ub4dc\ub97c \uc774\uc6a9\ud55c\ub2e4.\n\/\/\uc774 \uba54\uc11c\ub4dc\ub294 RealmQuery \uac1d\uccb4\ub97c \ubc18\ud658\ud558\uace0 \ub2e4\uc74c\uc5d0 \uc774\uc5b4\uc9c0\ub294 \uc870\uac74\uc744 \uc218\ud589\ud55c\ub2e4.\n\/\/\uc704\uc758 \ucf54\ub4dc\uc5d0\uc11c\ub294 max()\ub97c \uc870\uac74\uc73c\ub85c \ub2ec\uc558\uace0, max()\ub294 \ud604\uc7ac id\uc911 \uac00\uc7a5 \ud070 \uac12\uc744 \uc5bb\uc744 \ub54c \uc0ac\uc6a9\ud55c\ub2e4.\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>\ud504\ub85c\uc81d\ud2b8 \uc218\uc900\uc758 gradle\uc758 dependencies\uc5d0 \ub2e4\uc74c\uacfc \uac19\uc774 \ucd94\uac00 \ubaa8\ub4c8 \uc218\uc900\uc758 gradle \uc0c1\ub2e8\uc5d0 \ub2e4\uc74c \ucf54\ub4dc \ucd94\uac00 Todo \ubaa8\ub378 \ud074\ub798\uc2a4 \uc791\uc131 \uc0dd\uc874\ucf54\ub529 399, 400\ud398\uc774\uc9c0 \ucc38\uc870<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[349,372,7,4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1830"}],"collection":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1830"}],"version-history":[{"count":1,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1830\/revisions"}],"predecessor-version":[{"id":1831,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1830\/revisions\/1831"}],"wp:attachment":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}