{"id":106,"date":"2018-01-05T12:03:52","date_gmt":"2018-01-05T04:03:52","guid":{"rendered":"https:\/\/www.ray650128.com\/wordpress\/?p=106"},"modified":"2021-09-05T13:03:24","modified_gmt":"2021-09-05T05:03:24","slug":"realm%e8%b3%87%e6%96%99%e5%ba%ab%e5%ad%b8%e7%bf%92%e7%ad%86%e8%a8%98","status":"publish","type":"post","link":"https:\/\/blog.ray650128.com\/?p=106","title":{"rendered":"[Android]Realm\u8cc7\u6599\u5eab\u5b78\u7fd2\u7b46\u8a18"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u7c21\u4ecb<\/h2>\n\n\n\n<p>Realm\u662f\u4e00\u6b3e\u624b\u6a5f\u7684\u8cc7\u6599\u5eab\u5957\u4ef6\uff0c\u652f\u63f4Android\u3001iOS\u7cfb\u7d71\u3002<\/p>\n\n\n\n<p>Realm\u548c\u4ee5\u5f80\u7684ORM\u4e0d\u540c\uff0cRealm\u4f7f\u7528\u4e86\u81ea\u6709\u7684\u5f15\u64ce\u53d6\u4ee3SQLite\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Realm\u7684\u7279\u8272<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7c21\u6f54\uff1a\u4ee5\u5f80\u6211\u5011\u5728\u4f7f\u7528SQLite\u7684\u6642\u5019\uff0c\u5e38\u5e38\u9700\u8981\u5efa\u7acbSQLiteOpenHelper\u985e\u5225\uff0c\u4e26\u5be6\u4f5c\u4e00\u7cfb\u5217CRUD(\u65b0\u589e\u3001\u522a\u9664\u3001\u4fee\u6539\u3001\u67e5\u8a62)\u7684\u65b9\u6cd5\u3002\u800cReaml\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u65b9\u6cd5\u4f9b\u958b\u767c\u8005\u4f7f\u7528\uff0c\u56e0\u6b64\u7121\u9700\u518d\u81ea\u884c\u5be6\u4f5c\u4e00\u7cfb\u5217\u7684\u65b9\u6cd5\uff0c\u800c\u4e14\u63d0\u4f9b\u7684\u65b9\u6cd5\u975e\u5e38\u7c21\u55ae\u4e14\u5f37\u5927\uff0c\u8b93\u7a0b\u5f0f\u78bc\u53ef\u4ee5\u66f4\u7c21\u6f54\u3002<\/li><li>\u901f\u5ea6\u5feb\uff1aRealm\u4f7f\u7528\u81ea\u5bb6\u8cc7\u6599\u5eab\u5f15\u64ce\uff0c\u6838\u5fc3\u57fa\u65bcC++\u7de8\u5beb\u3002\u7531\u65bc\u4ed6\u662f\u91cd\u65b0\u958b\u767c\u7684\u8cc7\u6599\u5eab\u5be6\u73fe\uff0c\u56e0\u6b64\u548c\u73fe\u6709\u7684SQLite\u76f8\u6bd4\uff0c\u901f\u5ea6\u8f03\u5feb\u3002<\/li><li>\u652f\u63f4JSON\uff1aRealm\u652f\u63f4JSON\u683c\u5f0f\uff0c\u56e0\u6b64\u6211\u5011\u53ef\u4ee5\u4e0d\u9700\u8981\u5efa\u7acbModel\u985e\u5225\uff0c\u76f4\u63a5\u5f9eJSON\u683c\u5f0f\u8a2d\u5b9a\u8cc7\u6599\u3002<\/li><li>\u97ff\u61c9\u5f0f\u7a0b\u5f0f\u8a2d\u8a08\uff1aRealm\u53ef\u4ee5\u652f\u63f4\u89c0\u5bdf\u8cc7\u6599\u5eab\u4e2d\u7684\u8b8a\u66f4\uff0c\u7136\u5f8c\u66f4\u65b0UI\u756b\u9762\u3002\u4e5f\u53ef\u4ee5\u548cRxJava\u4e00\u8d77\u4f7f\u7528\u3002<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5728Android\u4f7f\u7528Realm<\/h2>\n\n\n\n<p>1.\u5728\u5c08\u6848\u6839\u76ee\u9304\u7684build.gradle\u4e2d\u52a0\u5165realm\u7684\u8def\u5f91\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">dependencies {\n    classpath \"io.realm:realm-gradle-plugin:1.0.0\"\n    ----\u4ee5\u4e0b\u7701\u7565----\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>2.\u5728app\u76ee\u9304\u7684build.gradle\u52a0\u5165realm\u7684plugin\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">apply plugin: 'realm-android'<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>3.\u5728Application\u985e\u5225\uff0c\u521d\u59cb\u5316Realm\u8cc7\u6599\u5eab\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">\/\/ \u521d\u59cb\u5316 Realm \u8cc7\u6599\u5eab\nRealm.init(this)\n\/\/ \u5efa\u7acb\u8cc7\u6599\u5eab\u53c3\u6578\u8a2d\u5b9a\nval config = RealmConfiguration.Builder().name(\"myDatabase.db\").build()\n\nRealm.setDefaultConfiguration(config)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>4.\u5efa\u7acbRealm\u8cc7\u6599\u6a21\u578b\u985e\u5225\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">open class MyDataModel : RealmObject() {\n    @PrimaryKey\n    var id: Int = 0\n    var name: String = \"\"\n    var telephone: String = \"\"\n    var email: String = \"\"\n}<\/code><\/pre>\n\n\n\n<p>\u5230\u76ee\u524d\u70ba\u6b62\uff0c\u5c07Realm\u5c0e\u5165\u5230Android\u5c08\u6848\u7684\u6b65\u9a5f\u5df2\u7d93\u5b8c\u6210\uff0c\u4ee5\u4e0b\u662f\u7c21\u55ae\u7684\u64cd\u4f5c\u793a\u7bc4\uff1a<\/p>\n\n\n\n<p><br>1.\u67e5\u8a62\u8cc7\u6599\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val realm = Realm.getDefaultInstance()\nval query = realm.where(MyDataModel::class.java)\nval result = query.findAll()<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>2.\u65b0\u589e\u8cc7\u6599\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">val realm = Realm.getDefaultInstance()\n\nrealm.executeTransaction { r -&gt;\n    val data = r.createObject(MyDataModel::class.java, getMaxID())\n    data.name = \"ABC\"\n    data.telephone = \"1234567890\"\n    data.email = \"abc@abc.com\"\n}\n\nrealm.close()<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>3.\u522a\u9664\u8cc7\u6599\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">val realm = Realm.getDefaultInstance()\nval query = realm.where(MyDataModel::class.java)\nval result = query.findAll()\n\nrealm.beginTransaction()\n\nval data = result[position]\ndata?.deleteFromRealm()\n\nrealm.commitTransaction()<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7d50\u5c3e<\/h2>\n\n\n\n<p>\u4ee5\u4e0a\u662fRealm\u8cc7\u6599\u5eab\u7684\u5c0e\u5165\u53ca\u57fa\u672c\u64cd\u4f5c\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p>\u9019\u88e1\u9644\u4e0a\u4e00\u4e9b\u53c3\u8003\u8cc7\u6599\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/realm.io\/\" data-type=\"URL\" data-id=\"https:\/\/realm.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">Realm\u5b98\u7db2<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/bng86.gitbooks.io\/android-third-party-\/content\/realm.html\" data-type=\"URL\" data-id=\"https:\/\/bng86.gitbooks.io\/android-third-party-\/content\/realm.html\" target=\"_blank\" rel=\"noreferrer noopener\">Android third-party\u4f7f\u7528\u5fc3\u5f97<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7c21\u4ecb Realm\u662f\u4e00\u6b3e\u624b\u6a5f\u7684\u8cc7\u6599\u5eab\u5957\u4ef6\uff0c\u652f\u63f4Android\u3001iOS\u7cfb\u7d71\u3002 Realm\u548c\u4ee5\u5f80\u7684ORM\u4e0d\u540c\uff0cRea &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,32],"class_list":["post-106","post","type-post","status-publish","format-standard","hentry","category-2","tag-android-app","tag-32"],"_links":{"self":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=106"}],"version-history":[{"count":5,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/106\/revisions"}],"predecessor-version":[{"id":1156,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/106\/revisions\/1156"}],"wp:attachment":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}