{"id":97176,"date":"2024-10-20T15:38:17","date_gmt":"2024-10-20T07:38:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97176\/"},"modified":"2024-10-20T15:38:18","modified_gmt":"2024-10-20T07:38:18","slug":"realm-ios-%e7%99%bc%e5%b8%83%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9b%b4%e6%96%b0%e9%80%9a%e7%9f%a5-realm-ios-%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9b%b4%e6%96%b0%e7%9a%84%e9%80%9a%e7%9f%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97176\/","title":{"rendered":"Realm iOS \u767c\u5e03\u6578\u64da\u5eab\u66f4\u65b0\u901a\u77e5 (realm ios \u6578\u64da\u5eab\u66f4\u65b0\u7684\u901a\u77e5)"},"content":{"rendered":"<h1 id=\"realm-ios-%e7%99%bc%e5%b8%83%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9b%b4%e6%96%b0%e9%80%9a%e7%9f%a5-MBeCcrEbtx\">Realm iOS \u767c\u5e03\u6578\u64da\u5eab\u66f4\u65b0\u901a\u77e5<\/h1>\n<p>\u5728\u79fb\u52d5\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u7ba1\u7406\u548c\u66f4\u65b0\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002Realm \u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684\u79fb\u52d5\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u548c\u6aa2\u7d22\u529f\u80fd\u3002\u96a8\u8457 iOS \u61c9\u7528\u7684\u6301\u7e8c\u767c\u5c55\uff0cRealm \u4e5f\u4e0d\u65b7\u63a8\u51fa\u66f4\u65b0\uff0c\u4ee5\u6eff\u8db3\u958b\u767c\u8005\u548c\u7528\u6236\u7684\u9700\u6c42\u3002\u672c\u6587\u5c07\u63a2\u8a0e Realm iOS \u6578\u64da\u5eab\u7684\u66f4\u65b0\u901a\u77e5\u53ca\u5176\u91cd\u8981\u6027\u3002<\/p>\n<h2 id=\"realm-%e6%95%b8%e6%93%9a%e5%ba%ab%e6%a6%82%e8%bf%b0-MBeCcrEbtx\">Realm \u6578\u64da\u5eab\u6982\u8ff0<\/h2>\n<p>Realm \u662f\u4e00\u500b\u958b\u6e90\u7684\u79fb\u52d5\u6578\u64da\u5eab\uff0c\u5c08\u70ba\u79fb\u52d5\u61c9\u7528\u8a2d\u8a08\u3002\u5b83\u7684\u4e3b\u8981\u7279\u9ede\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\uff1aRealm \u4f7f\u7528\u4e8c\u9032\u5236\u683c\u5f0f\u5b58\u5132\u6578\u64da\uff0c\u63d0\u4f9b\u5feb\u901f\u7684\u8b80\u53d6\u548c\u5beb\u5165\u6027\u80fd\u3002<\/li>\n<li>\u7c21\u55ae\u7684 API\uff1a\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u4f7f\u7528 Realm \u7684 API \u9032\u884c\u6578\u64da\u64cd\u4f5c\uff0c\u7121\u9700\u8907\u96dc\u7684 SQL \u8a9e\u53e5\u3002<\/li>\n<li>\u8de8\u5e73\u53f0\u652f\u6301\uff1a\u9664\u4e86 iOS\uff0cRealm \u9084\u652f\u6301 Android \u548c\u5176\u4ed6\u5e73\u53f0\uff0c\u65b9\u4fbf\u958b\u767c\u8005\u9032\u884c\u8de8\u5e73\u53f0\u958b\u767c\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9b%b4%e6%96%b0%e7%9a%84%e9%87%8d%e8%a6%81%e6%80%a7-MBeCcrEbtx\">\u6578\u64da\u5eab\u66f4\u65b0\u7684\u91cd\u8981\u6027<\/h2>\n<p>\u6578\u64da\u5eab\u7684\u66f4\u65b0\u5c0d\u65bc\u61c9\u7528\u7684\u7a69\u5b9a\u6027\u548c\u6027\u80fd\u81f3\u95dc\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u6578\u64da\u5eab\u66f4\u65b0\u7684\u4e00\u4e9b\u4e3b\u8981\u539f\u56e0\uff1a<\/p>\n<ul>\n<li><strong>\u4fee\u5fa9\u6f0f\u6d1e\uff1a<\/strong>\u96a8\u8457\u6642\u9593\u7684\u63a8\u79fb\uff0c\u53ef\u80fd\u6703\u767c\u73fe\u6578\u64da\u5eab\u4e2d\u7684\u6f0f\u6d1e\u6216\u932f\u8aa4\u3002\u53ca\u6642\u66f4\u65b0\u53ef\u4ee5\u4fee\u5fa9\u9019\u4e9b\u554f\u984c\uff0c\u4fdd\u8b77\u7528\u6236\u6578\u64da\u7684\u5b89\u5168\u3002<\/li>\n<li><strong>\u6027\u80fd\u512a\u5316\uff1a<\/strong>\u6bcf\u6b21\u66f4\u65b0\u901a\u5e38\u6703\u5305\u542b\u6027\u80fd\u6539\u9032\uff0c\u9019\u53ef\u4ee5\u63d0\u9ad8\u61c9\u7528\u7684\u97ff\u61c9\u901f\u5ea6\u548c\u7528\u6236\u9ad4\u9a57\u3002<\/li>\n<li><strong>\u65b0\u529f\u80fd\uff1a<\/strong>\u66f4\u65b0\u53ef\u80fd\u6703\u5f15\u5165\u65b0\u7684\u529f\u80fd\u6216 API\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7ba1\u7406\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2 id=\"realm-ios-%e6%9b%b4%e6%96%b0%e9%80%9a%e7%9f%a5%e7%9a%84%e5%af%a6%e7%8f%be-MBeCcrEbtx\">Realm iOS \u66f4\u65b0\u901a\u77e5\u7684\u5be6\u73fe<\/h2>\n<p>\u5728 iOS \u61c9\u7528\u4e2d\u5be6\u73fe Realm \u6578\u64da\u5eab\u7684\u66f4\u65b0\u901a\u77e5\uff0c\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u6b65\u9a5f\u9032\u884c\uff1a<\/p>\n<h3 id=\"1-%e7%9b%a3%e8%81%bd%e6%95%b8%e6%93%9a%e8%ae%8a%e6%9b%b4-MBeCcrEbtx\">1. \u76e3\u807d\u6578\u64da\u8b8a\u66f4<\/h3>\n<p>Realm \u63d0\u4f9b\u4e86\u901a\u77e5\u6a5f\u5236\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u76e3\u807d\u6578\u64da\u7684\u8b8a\u66f4\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>let realm = try! Realm()\nlet notificationToken = realm.observe { notification, realm in\n    switch notification {\n    case .didChange:\n        print(\"\u6578\u64da\u5eab\u5df2\u66f4\u65b0\")\n    default:\n        break\n    }\n}<\/code><\/pre>\n<h3 id=\"2-%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a%e5%ba%ab-MBeCcrEbtx\">2. \u66f4\u65b0\u6578\u64da\u5eab<\/h3>\n<p>\u7576\u9700\u8981\u66f4\u65b0\u6578\u64da\u5eab\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>try! realm.write {\n    realm.add(newObject)\n}<\/code><\/pre>\n<h3 id=\"3-%e9%80%9a%e7%9f%a5%e7%94%a8%e6%88%b6-MBeCcrEbtx\">3. \u901a\u77e5\u7528\u6236<\/h3>\n<p>\u5728\u6578\u64da\u5eab\u66f4\u65b0\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u672c\u5730\u901a\u77e5\u6216\u5176\u4ed6\u65b9\u5f0f\u901a\u77e5\u7528\u6236\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 UNUserNotificationCenter \u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>let content = UNMutableNotificationContent()\ncontent.title = \"\u6578\u64da\u5eab\u66f4\u65b0\"\ncontent.body = \"\u60a8\u7684\u6578\u64da\u5eab\u5df2\u6210\u529f\u66f4\u65b0\uff01\"\n\nlet request = UNNotificationRequest(identifier: \"dbUpdate\", content: content, trigger: nil)\nUNUserNotificationCenter.current().add(request, withCompletionHandler: nil)<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-MBeCcrEbtx\">\u7d50\u8ad6<\/h2>\n<p>Realm iOS \u6578\u64da\u5eab\u7684\u66f4\u65b0\u901a\u77e5\u5c0d\u65bc\u78ba\u4fdd\u61c9\u7528\u7684\u7a69\u5b9a\u6027\u548c\u6027\u80fd\u81f3\u95dc\u91cd\u8981\u3002\u901a\u904e\u76e3\u807d\u6578\u64da\u8b8a\u66f4\u3001\u66f4\u65b0\u6578\u64da\u5eab\u4ee5\u53ca\u901a\u77e5\u7528\u6236\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6236\u9ad4\u9a57\u3002\u96a8\u8457\u6280\u8853\u7684\u9032\u6b65\uff0c\u6301\u7e8c\u95dc\u6ce8 Realm \u7684\u66f4\u65b0\u52d5\u614b\u5c07\u6709\u52a9\u65bc\u958b\u767c\u8005\u66f4\u597d\u5730\u5229\u7528\u9019\u4e00\u5f37\u5927\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u6709\u95dc <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u548c\u5176\u4ed6\u670d\u52d9\u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7372\u53d6Realm iOS\u6578\u64da\u5eab\u66f4\u65b0\u901a\u77e5\uff0c\u96a8\u6642\u638c\u63e1\u6700\u65b0\u529f\u80fd\u8207\u6539\u9032\uff0c\u63d0\u5347\u60a8\u7684\u61c9\u7528\u6027\u80fd\u8207\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-97176","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=97176"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97176\/revisions"}],"predecessor-version":[{"id":97177,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97176\/revisions\/97177"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}