{"id":976,"date":"2023-03-11T20:39:33","date_gmt":"2023-03-11T12:39:33","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=976"},"modified":"2023-04-29T13:25:51","modified_gmt":"2023-04-29T05:25:51","slug":"difference-between-greendao-insertorreplace-and-save","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/difference-between-greendao-insertorreplace-and-save\/","title":{"rendered":"GreenDao insertOrReplace\u548csave\u7684\u533a\u522b"},"content":{"rendered":"<p>\u4f7f\u7528\u6570\u636e\u5e93\u63d2\u5165\u6570\u636e\u65f6\uff0c\u4f7f\u7528insert\u4f1a\u53d1\u751f\u56e0\u4e3akey\u503c\uff08\u901a\u5e38\u662fid\uff09\u91cd\u590d\u7684\u5f02\u5e38\u3002\u6211\u4eec\u5f53\u7136\u60f3\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\uff1a\u6709\u8be5\u6570\u636e\u65f6\u5219\u66f4\u65b0\uff0c\u6ca1\u6709\u8be5\u6570\u636e\u65f6\u5219\u63d2\u5165\u7684\u201c\u6709\u66f4\u65e0\u63d2\u201d\u7684\u65b9\u6cd5\uff0c\u5728GreenDao\u6709\u4e24\u4e2a\u65b9<code>insertOrReplace<\/code>\u548c<code>save<\/code>\uff0c\u53ef\u4e24\u8005\u662f\u6709\u533a\u522b\u7684<\/p>\n<ul>\n<li><code>insertOrReplace<\/code>\uff1a\u4f20\u5165\u7684\u5bf9\u8c61\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u6709\u5219\u66f4\u65b0\u65e0\u5219\u63d2\u5165\u3002\u63a8\u8350\u540c\u6b65\u6570\u636e\u5e93\u65f6\u4f7f\u7528\u8be5\u65b9\u6cd5\u3002<\/li>\n<li><code>save<\/code>\uff1a\u7c7b\u4f3c\u4e8einsertOrReplace\uff0c\u533a\u522b\u5728\u4e8esave\u4f1a\u5224\u65ad\u4f20\u5165\u5bf9\u8c61\u7684key\uff0c\u6709key\u7684\u5bf9\u8c61\u6267\u884c\u66f4\u65b0\uff0c\u65e0key\u7684\u6267\u884c\u63d2\u5165\u3002\u5f53\u5bf9\u8c61\u6709key\u4f46\u5e76\u4e0d\u5728\u6570\u636e\u5e93\u65f6\u4f1a\u6267\u884c\u5931\u8d25\uff0c\u9002\u7528\u4e8e\u4fdd\u5b58\u672c\u5730\u5217\u8868<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<h3>save\u65b9\u6cd5<\/h3>\n<pre><code class=\"language-java\">\/**\n * &quot;Saves&quot; an entity to the database: depending on the existence of the key property, it will be inserted\n * (key is null) or updated (key is not null).\n * &lt;p&gt;\n * This is similar to {@link #insertOrReplace(Object)}, but may be more efficient, because if a key is present,\n * it does not have to query if that key already exists.\n *\/\npublic void save(T entity) {\n    if (hasKey(entity)) {\n        update(entity);\n    } else {\n        insert(entity);\n    }\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u51fa\uff0csave\u53ea\u5224\u65ad\u4e86\u5bf9\u8c61key\u662f\u5426\u5b58\u5728\uff0c\u5e76\u4e0d\u4f1a\u53bb\u67e5\u8be2\u6570\u636e\u5e93\u3002\u5f53\u5bf9\u8c61\u6709key\u4f46\u5e76\u4e0d\u5728\u6570\u636e\u5e93\u65f6\uff0c\u6267\u884cupdate\u8bed\u53e5\u4e0d\u4f1a\u6709\u6570\u636e\u5f97\u5230\u66f4\u65b0\u3002<\/p>\n<h3>\u9002\u7528\u573a\u666f<\/h3>\n<ul>\n<li>\u53ea\u6709\u672c\u5730\u6570\u636e\u5e93\uff0c\u4e14key\u9ed8\u8ba4\u7531\u6570\u636e\u5e93\u751f\u6210\u3002\u76f4\u63a5\u4f7f\u7528save\u5c31\u597d<\/li>\n<li>\u63d2\u5165\u7684\u6570\u636e\u6709key\uff0c\u5176\u5b9e\u8fd9\u79cd\u60c5\u51b5\u901a\u5e38\u662f\u540c\u6b65\u7ebf\u4e0a\u6570\u636e\u5230\u672c\u5730\u6570\u636e\u5e93\u65f6\u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u76f4\u63a5\u4f7f\u7528\u4e86\u8fdc\u7a0b\u7684key\uff0c\u6240\u4ee5\u4e0d\u80fd\u4f7f\u7528save\uff0c\u5fc5\u987b\u4f7f\u7528insertOrReplace\u3002<\/li>\n<\/ul>\n<blockquote>\n<p>\u7ed3\u8bba\uff1a\u5728\u786e\u4fdd\u63d2\u5165\u6570\u636e\u6709key\u4e14\u5b58\u5728\u4e8e\u6570\u636e\u5e93\u7684\u60c5\u51b5\u4e0b\uff0c\u9002\u7528save\u66f4\u9ad8\u6548\u3002\u5176\u4ed6\u60c5\u51b5\u4e00\u5f8b\u9002\u7528insertOrReplace<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u6570\u636e\u5e93\u63d2\u5165\u6570\u636e\u65f6\uff0c\u4f7f\u7528insert\u4f1a\u53d1\u751f\u56e0\u4e3akey\u503c\uff08\u901a\u5e38\u662fid\uff09\u91cd\u590d\u7684\u5f02\u5e38\u3002\u6211\u4eec\u5f53\u7136\u60f3\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[205],"tags":[204],"class_list":["post-976","post","type-post","status-publish","format-standard","hentry","category-android-database-library","tag-greendao"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/976","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=976"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/976\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}