{"id":115288,"date":"2026-10-11T20:25:26","date_gmt":"2026-10-11T12:25:26","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/115288.html"},"modified":"2026-10-11T20:25:26","modified_gmt":"2026-10-11T12:25:26","slug":"android-datastore%e3%80%81sharedpreferences-%e4%b8%8e-mmkv","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/115288.html","title":{"rendered":"Android DataStore\u3001SharedPreferences \u4e0e MMKV"},"content":{"rendered":"<p>\u5bf9\u6bd4 SharedPreferences \u7684 XML \u4e0e apply\/commit\u3001DataStore \u7684 Flow \u548c\u4e8b\u52a1\u66f4\u65b0\u3001MMKV \u7684 mmap \u4e0e\u8ffd\u52a0\u5199&#xff0c;\u8bb2\u6e05\u591a\u8fdb\u7a0b\u3001\u6027\u80fd\u3001\u6301\u4e45\u6027\u548c\u8fc1\u79fb\u8fb9\u754c\u3002<\/p>\n<p>\u4e00\u4e2a\u6df1\u8272\u6a21\u5f0f\u5f00\u5173&#xff0c;SharedPreferences\u3001DataStore\u3001MMKV \u90fd\u80fd\u5b58&#xff1b;\u4f46\u5b83\u4eec\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u5f0f\u5e76\u4e0d\u76f8\u540c\u3002SharedPreferences&#xff08;\u4e0b\u6587\u7b80\u79f0 SP&#xff09;\u662f Android \u5e73\u53f0\u81ea\u5e26\u7684 XML \u952e\u503c\u5b58\u50a8&#xff0c;DataStore \u9762\u5411\u5f02\u6b65\u548c\u54cd\u5e94\u5f0f\u7684\u6570\u636e\u66f4\u65b0&#xff0c;\u817e\u8baf MMKV \u5219\u4ee5 mmap \u4e3a\u6838\u5fc3\u4f18\u5316\u5c0f\u578b\u952e\u503c\u8bfb\u5199\u3002\u8fd9\u91cc\u628a\u63d0\u95ee\u4e2d\u7684\u201cmkkv\u201d\u6309 MMKV \u7406\u89e3\u3002<\/p>\n<p>\u5224\u65ad\u8be5\u7528\u8c01&#xff0c;\u4e0d\u80fd\u53ea\u770b\u4e00\u5f20\u201c\u5199\u5165\u901f\u5ea6\u6392\u884c\u699c\u201d\u3002\u66f4\u91cd\u8981\u7684\u662f&#xff1a;\u8bfb\u5199\u662f\u5426\u4f1a\u5361\u4e3b\u7ebf\u7a0b&#xff1f;\u8c03\u7528\u8fd4\u56de\u65f6\u6570\u636e\u662f\u5426\u5df2\u6301\u4e45\u5316&#xff1f;\u662f\u5426\u8de8\u8fdb\u7a0b&#xff1f;\u6570\u636e\u662f\u5426\u6709\u7ed3\u6784\u548c\u4e8b\u52a1\u8981\u6c42&#xff1f;<\/p>\n<h3>\u4e00\u3001\u5148\u770b\u7ed3\u8bba&#xff1a;\u4e09\u8005\u5206\u522b\u9002\u5408\u4ec0\u4e48<\/h3>\n<table>\n<tr>\u7ef4\u5ea6SharedPreferences&#xff08;SP&#xff09;Jetpack DataStoreMMKV<\/tr>\n<tbody>\n<tr>\n<td>\u4e3b\u8981\u6570\u636e\u5f62\u6001<\/td>\n<td>\u5c11\u91cf\u57fa\u7840\u7c7b\u578b\u952e\u503c<\/td>\n<td>Preferences \u952e\u503c\u6216 Proto \u7c7b\u578b\u5316\u5bf9\u8c61<\/td>\n<td>\u5c11\u91cf\u57fa\u7840\u7c7b\u578b\u3001\u5b57\u7b26\u4e32\u3001\u5b57\u8282\u7b49\u952e\u503c<\/td>\n<\/tr>\n<tr>\n<td>\u5e38\u7528 API \u98ce\u683c<\/td>\n<td>\u540c\u6b65 get&#xff1b;apply \/ commit \u5199<\/td>\n<td>Flow \u8bfb&#xff1b;edit \/ updateData \u6302\u8d77\u5199<\/td>\n<td>\u540c\u6b65 decode \/ encode<\/td>\n<\/tr>\n<tr>\n<td>\u5178\u578b\u5e95\u5c42\u7ec4\u7ec7<\/td>\n<td>XML \u6587\u4ef6&#xff0c;\u8bfb\u53d6\u540e\u7ef4\u62a4\u8fdb\u7a0b\u5185\u952e\u503c Map&#xff0c;\u5199\u56de\u6587\u4ef6<\/td>\n<td>\u4e32\u884c\u4e8b\u52a1\u66f4\u65b0&#xff0c;\u5e8f\u5217\u5316\u5b58\u50a8\u5feb\u7167<\/td>\n<td>\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u3001\u4e8c\u8fdb\u5236\u7f16\u7801\u4e0e\u8ffd\u52a0\u5f0f\u66f4\u65b0<\/td>\n<\/tr>\n<tr>\n<td>\u591a\u8fdb\u7a0b<\/td>\n<td>\u4e0d\u5e94\u4f9d\u8d56\u65e7\u7684 MODE_MULTI_PROCESS \u83b7\u5f97\u4e00\u81f4\u6027<\/td>\n<td>\u663e\u5f0f\u4f7f\u7528\u591a\u8fdb\u7a0b DataStore \u5de5\u5382<\/td>\n<td>\u663e\u5f0f\u4f7f\u7528\u591a\u8fdb\u7a0b\u6a21\u5f0f<\/td>\n<\/tr>\n<tr>\n<td>\u54cd\u5e94\u5f0f\u89c2\u5bdf<\/td>\n<td>\u76d1\u542c\u5668&#xff0c;\u9700\u81ea\u884c\u7ba1\u7406\u751f\u547d\u5468\u671f<\/td>\n<td>\u5185\u5efa Flow<\/td>\n<td>\u539f\u751f\u8bfb\u5199 API \u4e0d\u662f Flow&#xff0c;\u9700\u81ea\u884c\u5305\u88c5\u89c2\u5bdf<\/td>\n<\/tr>\n<tr>\n<td>\u4f9d\u8d56<\/td>\n<td>\u5e73\u53f0\u5185\u7f6e<\/td>\n<td>AndroidX \u5e93<\/td>\n<td>\u7b2c\u4e09\u65b9\u539f\u751f\u5e93&#xff0c;\u542b native \u4ee3\u7801<\/td>\n<\/tr>\n<tr>\n<td>\u4f18\u5148\u8003\u8651\u7684\u573a\u666f<\/td>\n<td>\u7ef4\u62a4\u5c11\u91cf\u65e2\u6709\u914d\u7f6e<\/td>\n<td>\u65b0\u9879\u76ee\u7684\u7528\u6237\u8bbe\u7f6e\u3001\u7c7b\u578b\u5316\u5c0f\u578b\u72b6\u6001<\/td>\n<td>\u9ad8\u9891\u5c0f\u952e\u503c\u8bfb\u5199\u3001\u5df2\u6709 MMKV \u4f53\u7cfb\u6216\u660e\u786e\u7684\u591a\u8fdb\u7a0b\u9700\u6c42<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e0a\u8868\u662f\u9009\u578b\u8d77\u70b9&#xff0c;\u4e0d\u662f\u7edd\u5bf9\u6027\u80fd\u6392\u540d\u3002\u4e09\u8005\u90fd\u4e0d\u662f\u5173\u7cfb\u578b\u6570\u636e\u5e93&#xff1a;\u5217\u8868\u3001\u641c\u7d22\u3001\u5173\u8054\u3001\u5206\u9875\u6216\u5927\u91cf\u7ed3\u6784\u5316\u8bb0\u5f55\u5e94\u8003\u8651 Room \u7b49\u6570\u636e\u5e93&#xff0c;\u800c\u4e0d\u662f\u4e0d\u65ad\u5f80\u4e00\u4e2a\u952e\u503c\u6587\u4ef6\u91cc\u585e JSON\u3002<\/p>\n<h3>\u4e8c\u3001SP \u539f\u7406&#xff1a;XML &#043; \u8fdb\u7a0b\u5185\u952e\u503c Map&#xff0c;\u4e3a\u4ec0\u4e48 apply() \u4ecd\u4f1a\u5361&#xff1f;<\/h3>\n<p><span class=\"token keyword\">val<\/span> sp <span class=\"token operator\">&#061;<\/span> context<span class=\"token punctuation\">.<\/span><span class=\"token function\">getSharedPreferences<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-literal singleline\"><span class=\"token string\">&#034;settings&#034;<\/span><\/span><span class=\"token punctuation\">,<\/span> Context<span class=\"token punctuation\">.<\/span>MODE_PRIVATE<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">val<\/span> enabled <span class=\"token operator\">&#061;<\/span> sp<span class=\"token punctuation\">.<\/span><span class=\"token function\">getBoolean<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-literal singleline\"><span class=\"token string\">&#034;dark_mode&#034;<\/span><\/span><span class=\"token punctuation\">,<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span><br \/>\nsp<span class=\"token punctuation\">.<\/span><span class=\"token function\">edit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">putBoolean<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-literal singleline\"><span class=\"token string\">&#034;dark_mode&#034;<\/span><\/span><span class=\"token punctuation\">,<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">apply<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>SP \u4f1a\u628a XML \u5185\u5bb9\u8bfb\u5165\u8fdb\u7a0b\u5185\u7684\u952e\u503c\u6620\u5c04&#xff0c;\u4e4b\u540e\u5e38\u89c1\u7684 get \u4ece\u5185\u5b58\u53d6\u503c\u3002\u9996\u6b21\u8bbf\u95ee\u82e5\u6587\u4ef6\u5c1a\u672a\u52a0\u8f7d\u5b8c&#xff0c;\u8c03\u7528\u65b9\u4ecd\u53ef\u80fd\u7b49\u5f85\u52a0\u8f7d&#xff1b;\u201c\u8bfb\u53d6\u5e73\u65f6\u5f88\u5feb\u201d\u4e0d\u4ee3\u8868\u51b7\u542f\u52a8\u5b8c\u5168\u6ca1\u6709 I\/O \u6210\u672c\u3002<\/p>\n<p>\u5199\u5165\u65f6 Editor \u5148\u4fee\u6539\u5185\u5b58\u4e2d\u7684\u503c&#xff0c;\u518d\u5b89\u6392\u78c1\u76d8\u843d\u76d8\u3002SP \u6587\u4ef6\u5e76\u975e\u4e3a\u6bcf\u4e2a key \u5355\u72ec\u5efa\u7d22\u5f15\u548c\u589e\u91cf\u6570\u636e\u9875&#xff1b;\u4e00\u6b21\u4fee\u6539\u901a\u5e38\u9700\u8981\u628a\u5f53\u524d\u504f\u597d\u5185\u5bb9\u91cd\u65b0\u5199\u6210 XML \u6587\u4ef6\u3002\u5e73\u53f0\u5b9e\u73b0\u4f1a\u91c7\u53d6\u6587\u4ef6\u5907\u4efd\/\u66ff\u6362\u7b49\u63aa\u65bd\u964d\u4f4e\u635f\u574f\u98ce\u9669&#xff0c;\u4f46\u4e0d\u80fd\u628a\u5b83\u7406\u89e3\u6210\u53ef\u8de8\u591a\u4e2a\u4e1a\u52a1\u5bf9\u8c61\u7684\u6570\u636e\u5e93\u4e8b\u52a1\u3002<\/p>\n<h4>apply() \u4e0e commit() \u7684\u771f\u6b63\u533a\u522b<\/h4>\n<table>\n<tr>\u8c03\u7528\u5185\u5b58\u4f55\u65f6\u66f4\u65b0\u78c1\u76d8\u5199\u5165\u8fd4\u56de\u503c<\/tr>\n<tbody>\n<tr>\n<td>apply()<\/td>\n<td>\u8c03\u7528\u671f\u95f4\u5148\u66f4\u65b0<\/td>\n<td>\u5f02\u6b65\u6392\u961f<\/td>\n<td>\u65e0\u6210\u529f\u5e03\u5c14\u503c<\/td>\n<\/tr>\n<tr>\n<td>commit()<\/td>\n<td>\u8c03\u7528\u671f\u95f4\u66f4\u65b0<\/td>\n<td>\u8c03\u7528\u65b9\u7b49\u5f85\u5199\u5165\u5b8c\u6210<\/td>\n<td>Boolean<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>apply() \u8fd4\u56de\u540e&#xff0c;\u5f53\u524d\u8fdb\u7a0b\u8bfb\u5230\u7684\u65b0\u503c\u901a\u5e38\u5df2\u7ecf\u751f\u6548&#xff0c;\u4f46\u4e0d\u80fd\u636e\u6b64\u8bc1\u660e\u78c1\u76d8\u5df2\u5b8c\u6210\u6301\u4e45\u5316&#xff1b;\u8fdb\u7a0b\u7a81\u7136\u7ec8\u6b62\u65f6\u6709\u4e22\u5931\u6700\u8fd1\u66f4\u6539\u7684\u98ce\u9669\u3002commit() \u7b49\u5f85\u78c1\u76d8\u5199\u5165&#xff0c;\u4e0d\u8be5\u5728\u4e3b\u7ebf\u7a0b\u5927\u91cf\u8c03\u7528\u3002apply() \u4e5f\u4e0d\u662f\u201c\u6c38\u8fdc\u6ca1\u6709\u4e3b\u7ebf\u7a0b\u6210\u672c\u201d&#xff1a;\u5927\u91cf\u5f85\u843d\u76d8\u5de5\u4f5c\u53ef\u80fd\u5728\u751f\u547d\u5468\u671f\u5207\u6362\u7b49\u65f6\u673a\u7b49\u5f85\u6536\u5c3e&#xff0c;\u9020\u6210\u5361\u987f\u751a\u81f3 ANR \u98ce\u9669\u3002<\/p>\n<p>SP \u7684\u65e7\u591a\u8fdb\u7a0b\u6a21\u5f0f\u4e0d\u63d0\u4f9b\u53ef\u4f9d\u8d56\u7684\u5b9e\u65f6\u4e00\u81f4\u6027\u3002\u4e24\u4e2a\u8fdb\u7a0b\u5404\u81ea\u6301\u6709\u4e00\u4efd\u5185\u5b58\u4e2d\u7684\u952e\u503c Map \u65f6&#xff0c;\u4e00\u4e2a\u8fdb\u7a0b\u66f4\u65b0\u5e76\u4e0d\u610f\u5473\u7740\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u7acb\u523b\u770b\u5230&#xff1b;\u65b0\u65b9\u6848\u4e0d\u8981\u628a\u5b83\u5f53\u4f5c\u8de8\u8fdb\u7a0b\u5171\u4eab\u6570\u636e\u5e93\u3002<\/p>\n<h3>\u4e09\u3001DataStore \u539f\u7406&#xff1a;\u4e32\u884c\u66f4\u65b0 &#043; Flow &#043; \u6301\u4e45\u5316\u4e8b\u52a1<\/h3>\n<p>DataStore \u6709\u4e24\u79cd\u5e38\u89c1\u5f62\u6001&#xff1a;Preferences DataStore \u7c7b\u4f3c\u7c7b\u578b\u5b89\u5168 key \u7684\u952e\u503c\u8bbe\u7f6e&#xff1b;Proto DataStore \u4f7f\u7528 Protocol Buffers schema \u5b9a\u4e49\u5b8c\u6574\u7684\u6570\u636e\u5bf9\u8c61\u3002\u4e24\u8005\u5171\u4eab DataStore \u7684\u5f02\u6b65\u66f4\u65b0\u6a21\u578b&#xff0c;\u4f46 Proto \u66f4\u9002\u5408\u6709\u7a33\u5b9a\u7ed3\u6784\u3001\u9700\u8981 schema \u6f14\u8fdb\u7684\u5c0f\u578b\u914d\u7f6e\u3002<\/p>\n<p><span class=\"token keyword\">private<\/span> <span class=\"token keyword\">val<\/span> Context<span class=\"token punctuation\">.<\/span>settingsStore <span class=\"token keyword\">by<\/span> <span class=\"token function\">preferencesDataStore<\/span><span class=\"token punctuation\">(<\/span>name <span class=\"token operator\">&#061;<\/span> <span class=\"token string-literal singleline\"><span class=\"token string\">&#034;settings&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">private<\/span> <span class=\"token keyword\">val<\/span> DARK_MODE <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">booleanPreferencesKey<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-literal singleline\"><span class=\"token string\">&#034;dark_mode&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">val<\/span> darkMode<span class=\"token operator\">:<\/span> Flow<span class=\"token operator\">&lt;<\/span>Boolean<span class=\"token operator\">&gt;<\/span> <span class=\"token operator\">&#061;<\/span> context<span class=\"token punctuation\">.<\/span>settingsStore<span class=\"token punctuation\">.<\/span>data<br \/>\n    <span class=\"token punctuation\">.<\/span><span class=\"token function\">map<\/span> <span class=\"token punctuation\">{<\/span> preferences <span class=\"token operator\">-&gt;<\/span> preferences<span class=\"token punctuation\">[<\/span>DARK_MODE<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">?:<\/span> <span class=\"token boolean\">false<\/span> <span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">suspend<\/span> <span class=\"token keyword\">fun<\/span> <span class=\"token function\">setDarkMode<\/span><span class=\"token punctuation\">(<\/span>context<span class=\"token operator\">:<\/span> Context<span class=\"token punctuation\">,<\/span> enabled<span class=\"token operator\">:<\/span> Boolean<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    context<span class=\"token punctuation\">.<\/span>settingsStore<span class=\"token punctuation\">.<\/span><span class=\"token function\">edit<\/span> <span class=\"token punctuation\">{<\/span> preferences <span class=\"token operator\">-&gt;<\/span><br \/>\n        preferences<span class=\"token punctuation\">[<\/span>DARK_MODE<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> enabled<br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>DataStore \u5c06\u5bf9\u540c\u4e00\u5b58\u50a8\u7684\u66f4\u65b0\u4e32\u884c\u5904\u7406&#xff0c;\u5728 edit \/ updateData \u7684\u53d8\u6362\u4e2d\u8bfb\u53d6\u5f53\u524d\u72b6\u6001\u3001\u4fee\u6539\u5e76\u63d0\u4ea4&#xff1b;\u6210\u529f\u8fd4\u56de\u610f\u5473\u7740\u8fd9\u6b21\u66f4\u65b0\u5df2\u7ecf\u7531 DataStore \u5199\u5165\u5176\u6301\u4e45\u5b58\u50a8\u3002data \u662f Flow&#xff0c;\u8ba2\u9605\u8005\u53ef\u4ee5\u89c2\u5bdf\u540e\u7eed\u72b6\u6001&#xff0c;\u800c\u4e0d\u5fc5\u624b\u5de5\u7ef4\u62a4 SP \u76d1\u542c\u5668\u4e0e\u7ebf\u7a0b\u5207\u6362\u3002UI \u4e2d\u5e94\u6309\u751f\u547d\u5468\u671f\u6536\u96c6\u5b83&#xff0c;\u907f\u514d\u91cd\u590d\u542f\u52a8\u65e0\u4e3b\u6536\u96c6\u5668\u3002<\/p>\n<p>DataStore \u7684\u4f18\u52bf\u662f\u4e00\u81f4\u7684\u66f4\u65b0\u8bed\u4e49\u548c\u5f02\u6b65 API&#xff0c;\u4e0d\u662f\u201c\u6bcf\u6b21\u53ea\u4fee\u6539\u6587\u4ef6\u91cc\u7684\u4e00\u4e2a\u5b57\u8282\u201d\u3002Preferences \u6216 Proto \u66f4\u65b0\u4ecd\u6d89\u53ca\u5bf9\u6570\u636e\u5bf9\u8c61\u7684\u5e8f\u5217\u5316\u548c\u6587\u4ef6\u5199\u5165&#xff1b;\u6587\u4ef6\u4e0d\u65ad\u53d8\u5927\u3001\u9ad8\u9891\u5199\u5165\u5927\u91cf\u5bf9\u8c61\u65f6&#xff0c;DataStore \u4e0d\u662f\u6570\u636e\u5e93\u7684\u66ff\u4ee3\u54c1\u3002\u4e5f\u4e0d\u8981\u4e3a\u540c\u4e00\u4e2a\u5e95\u5c42\u6587\u4ef6\u521b\u5efa\u591a\u4e2a\u4e92\u76f8\u72ec\u7acb\u7684 DataStore \u5b9e\u4f8b\u3002<\/p>\n<h4>\u591a\u8fdb\u7a0b\u4e0d\u662f\u9ed8\u8ba4\u81ea\u52a8\u5b8c\u6210\u7684<\/h4>\n<p>\u9700\u8981\u8de8\u8fdb\u7a0b\u8bfb\u5199\u540c\u4e00\u4efd\u6570\u636e\u65f6&#xff0c;\u5e94\u6309 AndroidX \u7684\u591a\u8fdb\u7a0b DataStore API&#xff08;\u4f8b\u5982 MultiProcessDataStoreFactory&#xff09;\u914d\u7f6e&#xff0c;\u5e76\u4fdd\u8bc1\u5404\u8fdb\u7a0b\u6307\u5411\u540c\u4e00\u6587\u4ef6\u3001\u4f7f\u7528\u4e00\u81f4\u7684\u6570\u636e\u683c\u5f0f\u3002\u4e0d\u8981\u5bf9\u540c\u4e00\u6587\u4ef6\u6df7\u7528\u5355\u8fdb\u7a0b\u4e0e\u591a\u8fdb\u7a0b DataStore&#xff0c;\u6216\u540c\u65f6\u8ba9 SP\/MMKV \u53bb\u5199\u90a3\u4efd DataStore \u6587\u4ef6\u3002\u591a\u8fdb\u7a0b\u652f\u6301\u89e3\u51b3\u7684\u662f\u8fdb\u7a0b\u95f4\u4e00\u81f4\u66f4\u65b0&#xff0c;\u4e0d\u662f\u8de8\u8bbe\u5907\u540c\u6b65\u3002<\/p>\n<h4>Proto \u548c Preferences \u600e\u4e48\u9009&#xff1f;<\/h4>\n<ul>\n<li>Preferences&#xff1a;\u5c11\u91cf\u72ec\u7acb\u8bbe\u7f6e\u9879&#xff0c;\u8fc1\u79fb\u65e7 SP \u8f83\u76f4\u63a5&#xff1b;key \u4e0e\u503c\u7684\u4e1a\u52a1\u7ea6\u675f\u4ecd\u9700\u81ea\u5df1\u8bbe\u8ba1\u3002<\/li>\n<li>Proto&#xff1a;\u4e00\u7ec4\u6709\u5173\u8054\u7684\u7c7b\u578b\u5316\u914d\u7f6e\u3001\u9ed8\u8ba4\u503c\u548c schema \u6f14\u8fdb\u8981\u6c42&#xff1b;\u9700\u8981\u7ef4\u62a4 .proto \u4e0e\u5e8f\u5217\u5316\u5668\u3002<\/li>\n<\/ul>\n<p>\u4e24\u8005\u90fd\u9002\u5408\u5c0f\u578b\u8bbe\u7f6e\u6216\u72b6\u6001&#xff0c;\u4e0d\u662f\u7f13\u5b58\u5927\u56fe\u7247\u3001\u89c6\u9891\u6587\u4ef6\u548c\u53ef\u67e5\u8be2\u8bb0\u5f55\u7684\u5730\u65b9\u3002DataStore \u8fd8\u63d0\u4f9b\u4ece\u65e7 SP \u8fc1\u79fb\u7684\u8f85\u52a9\u673a\u5236&#xff1b;\u8fc1\u79fb\u540e\u5e94\u505c\u6b62\u7ee7\u7eed\u5411\u65e7 SP \u5199\u540c\u4e00\u4e1a\u52a1\u914d\u7f6e&#xff0c;\u5426\u5219\u65b0\u65e7\u6765\u6e90\u4f1a\u5206\u53c9\u3002<\/p>\n<h3>\u56db\u3001MMKV \u539f\u7406&#xff1a;mmap\u3001\u4e8c\u8fdb\u5236\u7f16\u7801\u4e0e\u8ffd\u52a0\u5199<\/h3>\n<p>MMKV<span class=\"token punctuation\">.<\/span><span class=\"token function\">initialize<\/span><span class=\"token punctuation\">(<\/span>context<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">val<\/span> kv <span class=\"token operator\">&#061;<\/span> MMKV<span class=\"token punctuation\">.<\/span><span class=\"token function\">mmkvWithID<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-literal singleline\"><span class=\"token string\">&#034;settings&#034;<\/span><\/span><span class=\"token punctuation\">,<\/span> MMKV<span class=\"token punctuation\">.<\/span>SINGLE_PROCESS_MODE<span class=\"token punctuation\">)<\/span><br \/>\nkv<span class=\"token punctuation\">.<\/span><span class=\"token function\">encode<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-literal singleline\"><span class=\"token string\">&#034;dark_mode&#034;<\/span><\/span><span class=\"token punctuation\">,<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">val<\/span> enabled <span class=\"token operator\">&#061;<\/span> kv<span class=\"token punctuation\">.<\/span><span class=\"token function\">decodeBool<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-literal singleline\"><span class=\"token string\">&#034;dark_mode&#034;<\/span><\/span><span class=\"token punctuation\">,<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>MMKV \u7684\u6838\u5fc3\u662f memory-mapped file&#xff08;\u5185\u5b58\u6620\u5c04\u6587\u4ef6&#xff09;&#xff1a;\u901a\u8fc7 mmap \u628a\u6587\u4ef6\u6620\u5c04\u5230\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4&#xff0c;\u8bfb\u5199\u53ef\u76f4\u63a5\u64cd\u4f5c\u6620\u5c04\u533a\u57df&#xff0c;\u51cf\u5c11\u4f20\u7edf\u6587\u4ef6 I\/O \u8def\u5f84\u4e0a\u7684\u590d\u5236\u4e0e\u7cfb\u7edf\u8c03\u7528\u3002\u5b83\u91c7\u7528\u7d27\u51d1\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\u5316\u548c\u8ffd\u52a0\u5f0f\u66f4\u65b0&#xff0c;\u7a7a\u95f4\u4e0d\u8db3\u6216\u65e0\u6548\u8bb0\u5f55\u589e\u591a\u65f6\u9700\u8981\u6269\u5bb9\u3001\u6574\u7406\/\u56de\u5199&#xff1b;\u5e76\u975e\u201c\u6bcf\u6b21\u4fee\u6539\u90fd\u91cd\u5199\u6574\u4efd XML\u201d\u3002<\/p>\n<p>MMKV \u8fd8\u4f1a\u8fdb\u884c\u5b8c\u6574\u6027\u68c0\u67e5&#xff0c;\u5e76\u63d0\u4f9b\u591a\u8fdb\u7a0b\u6a21\u5f0f\u4e0b\u7684\u540c\u6b65\u673a\u5236\u3002\u4f46\u51e0\u4e2a\u5bb9\u6613\u88ab\u8bef\u8bfb\u7684\u70b9\u5fc5\u987b\u5206\u5f00&#xff1a;<\/p>\n<ul>\n<li>**\u6620\u5c04\u5230\u5730\u5740\u7a7a\u95f4\u4e0d\u7b49\u4e8e\u6574\u4e2a\u6587\u4ef6\u4e00\u76f4\u5360\u7528\u7b49\u91cf\u7269\u7406\u5185\u5b58\u3002**\u9875\u9762\u6309\u9700\u8fdb\u5165\u5185\u5b58&#xff0c;\u4e5f\u53ef\u80fd\u53d1\u751f\u7f3a\u9875\u4e0e\u78c1\u76d8 I\/O\u3002<\/li>\n<li>**encode() \u8fd4\u56de\u4e0d\u7b49\u4e8e\u6bcf\u4e2a\u5b57\u8282\u90fd\u5df2\u540c\u6b65\u5230\u6301\u4e45\u4ecb\u8d28\u3002**\u810f\u9875\u7684\u5237\u76d8\u7531\u7cfb\u7edf\u4e0e\u5e93\u7684\u540c\u6b65\u7b56\u7565\u5171\u540c\u51b3\u5b9a&#xff1b;\u5173\u952e\u5199\u5165\u5e94\u9605\u8bfb\u6240\u7528 MMKV \u7248\u672c\u7684\u540c\u6b65 API \u4e0e\u5d29\u6e83\u6062\u590d\u8bf4\u660e\u3002<\/li>\n<li>**\u591a\u8fdb\u7a0b\u6a21\u5f0f\u4e0d\u7b49\u4e8e\u4e1a\u52a1\u4e8b\u52a1\u3002**\u591a\u4e2a\u8fdb\u7a0b\u8bfb\u5199\u9700\u8981\u663e\u5f0f\u4f7f\u7528\u5bf9\u5e94\u6a21\u5f0f&#xff1b;\u8de8\u591a\u4e2a key \u7684\u4e1a\u52a1\u4e0d\u53d8\u91cf\u4ecd\u8981\u81ea\u5df1\u8bbe\u8ba1\u3002<\/li>\n<li>**\u539f\u751f\u5e93\u4e0d\u662f\u96f6\u6210\u672c\u3002**\u521d\u59cb\u5316\u3001ABI \u4ea7\u7269\u3001\u5347\u7ea7\u517c\u5bb9\u3001\u6587\u4ef6\u8fc1\u79fb\u548c\u6392\u67e5 native \u95ee\u9898\u90fd\u662f\u5de5\u7a0b\u6210\u672c\u3002<\/li>\n<\/ul>\n<p>MMKV \u63d0\u4f9b\u53ef\u9009\u52a0\u5bc6\u80fd\u529b&#xff0c;\u4f46\u52a0\u5bc6\u5f00\u5173\u5e76\u4e0d\u81ea\u52a8\u89e3\u51b3\u5bc6\u94a5\u4fdd\u7ba1\u3001\u8f6e\u6362\u3001\u5907\u4efd\u6216\u8bbe\u5907\u8fc1\u79fb\u95ee\u9898\u3002\u767b\u5f55\u4ee4\u724c\u7b49\u654f\u611f\u6570\u636e\u5e94\u5148\u505a\u5a01\u80c1\u6a21\u578b\u548c Android Keystore \u76f8\u5173\u8bbe\u8ba1&#xff1b;SP\u3001DataStore\u3001MMKV \u7684\u666e\u901a\u6587\u4ef6\u5f62\u5f0f\u90fd\u4e0d\u5e94\u88ab\u5f53\u6210\u201c\u5929\u7136\u5b89\u5168\u4fdd\u9669\u7bb1\u201d\u3002<\/p>\n<h3>\u4e94\u3001\u4e3a\u4ec0\u4e48\u201cMMKV \u6bd4 SP \u5feb\u201d\u4e0d\u80fd\u6210\u4e3a\u552f\u4e00\u7406\u7531&#xff1f;<\/h3>\n<p>\u5e38\u89c1\u57fa\u51c6\u6d4b\u8bd5\u628a\u51e0\u4e07\u6b21\u5199\u5165\u653e\u5728\u540c\u4e00\u8fdb\u7a0b\u3001\u540c\u4e00\u4e2a\u70ed\u7f13\u5b58\u73af\u5883\u4e2d&#xff0c;\u7136\u540e\u53ea\u6bd4\u8f83 API \u8c03\u7528\u8017\u65f6\u3002\u8fd9\u6837\u7684\u6d4b\u8bd5\u53ef\u80fd\u8bf4\u660e\u8be5\u573a\u666f\u7684\u70ed\u8def\u5f84\u5f00\u9500&#xff0c;\u5374\u4e0d\u80fd\u76f4\u63a5\u63a8\u5bfc\u51b7\u542f\u52a8\u3001\u5f3a\u5236\u6301\u4e45\u5316\u3001\u591a\u8fdb\u7a0b\u6216\u8fdb\u7a0b\u88ab\u6740\u540e\u7684\u8868\u73b0\u3002<\/p>\n<p>\u4e00\u4e2a\u66f4\u516c\u5e73\u7684\u6d4b\u8bd5\u81f3\u5c11\u8981\u56fa\u5b9a&#xff1a;\u540c\u4e00\u8bbe\u5907\u4e0e Android \u7248\u672c\u3001\u6570\u636e\u91cf\u548c key \u5206\u5e03\u3001\u51b7\u8bfb\/\u70ed\u8bfb\u3001\u5355\u6b21\u5199\/\u6279\u91cf\u5199\u3001\u662f\u5426\u7b49\u5f85\u6301\u4e45\u5316\u3001\u8fdb\u7a0b\u91cd\u542f\u540e\u662f\u5426\u4ecd\u80fd\u8bfb\u5230\u3001\u6587\u4ef6\u53d8\u5927\u540e\u7684\u6574\u7406\u6210\u672c\u3002DataStore \u7684\u6302\u8d77\u5f0f\u4e8b\u52a1\u3001SP \u7684 apply\u3001MMKV \u7684\u666e\u901a encode \u82e5\u6301\u4e45\u5316\u8bed\u4e49\u4e0d\u540c&#xff0c;\u76f4\u63a5\u6bd4\u8f83\u201c\u8fd4\u56de\u7528\u65f6\u201d\u5e76\u4e0d\u516c\u5e73\u3002<\/p>\n<p>\u771f\u5b9e\u9879\u76ee\u9009\u578b\u53ef\u6309\u8fd9\u4e2a\u987a\u5e8f&#xff1a;<\/p>\n<li>**\u65b0\u9875\u9762\u8bbe\u7f6e\u9700\u8981 Flow\u3001\u539f\u5b50\u66f4\u65b0\u548c\u53ef\u7ef4\u62a4 schema&#xff1f;**\u4f18\u5148 DataStore\u3002<\/li>\n<li>**\u7ef4\u62a4\u65e2\u6709 SP\u3001\u6570\u636e\u91cf\u5f88\u5c0f\u4e14\u6ca1\u6709\u660e\u663e\u5361\u987f&#xff1f;**\u4e0d\u5fc5\u4e3a\u4e86\u53e3\u53f7\u7acb\u5373\u5168\u91cf\u8fc1\u79fb&#xff1b;\u5148\u628a\u4e3b\u7ebf\u7a0b commit() \u548c\u9519\u8bef\u7684\u591a\u8fdb\u7a0b\u7528\u6cd5\u4fee\u6389\u3002<\/li>\n<li>**\u5927\u91cf\u5c0f\u578b\u952e\u503c\u9891\u7e41\u8bfb\u5199&#xff0c;\u5df2\u786e\u8ba4\u74f6\u9888\u4e14\u53ef\u627f\u62c5 native \u4f9d\u8d56&#xff1f;**\u8bc4\u4f30 MMKV&#xff0c;\u5e76\u6d4b\u51b7\u542f\u52a8\u3001\u6301\u4e45\u6027\u548c\u591a\u8fdb\u7a0b\u3002<\/li>\n<li>**\u9700\u8981\u67e5\u8be2\u3001\u5173\u8054\u3001\u4e8b\u52a1\u6216\u5927\u91cf\u8bb0\u5f55&#xff1f;**\u76f4\u63a5\u770b Room\/SQLite&#xff0c;\u800c\u4e0d\u662f\u5728\u4e09\u8005\u4e4b\u95f4\u786c\u9009\u3002<\/li>\n<h3>\u516d\u3001\u8fc1\u79fb\u65f6\u6700\u5bb9\u6613\u9057\u6f0f\u7684\u4e8b<\/h3>\n<p>\u4ece SP \u8f6c DataStore&#xff0c;\u53ef\u5229\u7528 SharedPreferencesMigration \u5728\u9996\u6b21\u8bbf\u95ee\u65f6\u8bfb\u53d6\u65e7\u503c\u3002\u8fc1\u79fb\u8981\u660e\u786e\u54ea\u4e9b key\u3001\u7f3a\u7701\u503c\u5982\u4f55\u89e3\u91ca\u3001\u65e7\u6587\u4ef6\u4f55\u65f6\u5220\u9664&#xff0c;\u4ee5\u53ca\u65e7\u7248\u672c\u7ec4\u4ef6\u662f\u5426\u4ecd\u5199 SP\u3002\u4e0d\u8981\u8ba9\u65b0\u9875\u9762\u8bfb DataStore\u3001\u65e7\u540e\u53f0\u4efb\u52a1\u7ee7\u7eed\u5199 SP&#xff0c;\u5374\u4ee5\u4e3a\u4e8c\u8005\u81ea\u52a8\u540c\u6b65\u3002<\/p>\n<p>\u4ece SP \u8f6c MMKV \u4e5f\u8981\u5148\u89c4\u5212\u4e00\u6b21\u6027\u6570\u636e\u5bfc\u5165\u3001\u7248\u672c\u6807\u8bb0\u548c\u5931\u8d25\u56de\u9000&#xff1b;\u907f\u514d\u6bcf\u6b21\u542f\u52a8\u91cd\u590d\u5bfc\u5165&#xff0c;\u628a\u7528\u6237\u65b0\u503c\u8986\u76d6\u6210\u65e7\u503c\u3002\u65e0\u8bba\u8fc1\u79fb\u5230\u54ea\u79cd\u5b58\u50a8&#xff0c;\u90fd\u5e94\u5728\u771f\u5b9e\u8bbe\u5907\u4e0a\u8986\u76d6\u201c\u65e7\u7248\u5b89\u88c5 \u2192 \u5347\u7ea7 \u2192 \u4fee\u6539\u914d\u7f6e \u2192 \u6740\u8fdb\u7a0b\u91cd\u542f\u201d\u8fd9\u6761\u8def\u5f84\u3002<\/p>\n<p>\u5982\u679c\u5df2\u6709\u591a\u8fdb\u7a0b\u670d\u52a1&#xff0c;\u5148\u753b\u51fa\u6bcf\u4e2a\u8fdb\u7a0b\u5b9e\u9645\u8bfb\u5199\u7684 key&#xff0c;\u518d\u51b3\u5b9a\u5355\u8fdb\u7a0b DataStore\u3001\u591a\u8fdb\u7a0b DataStore \u6216 MMKV \u591a\u8fdb\u7a0b\u6a21\u5f0f&#xff1b;\u4e0d\u8981\u7528\u4e00\u6b21\u5355\u8fdb\u7a0b\u5355\u6d4b\u4ee3\u66ff\u8de8\u8fdb\u7a0b\u9a8c\u8bc1\u3002<\/p>\n<h3>\u4e03\u3001\u9762\u8bd5\u9ad8\u9891\u95ee\u7b54<\/h3>\n<p>Q1&#xff1a;apply() \u4e0e commit() \u6709\u4ec0\u4e48\u533a\u522b&#xff1f; apply \u5148\u66f4\u65b0\u5185\u5b58\u3001\u5f02\u6b65\u5b89\u6392\u78c1\u76d8\u5199\u5165\u4e14\u4e0d\u8fd4\u56de\u6210\u529f\u72b6\u6001&#xff1b;commit \u7b49\u5f85\u78c1\u76d8\u5199\u5165\u5e76\u8fd4\u56de\u5e03\u5c14\u503c\u3002\u4e24\u8005\u90fd\u4e0d\u9002\u5408\u6beb\u65e0\u8282\u5236\u5730\u5728\u4e3b\u7ebf\u7a0b\u9ad8\u9891\u5199\u5927\u6570\u636e\u3002<\/p>\n<p>Q2&#xff1a;DataStore \u4e3a\u4ec0\u4e48\u80fd\u907f\u514d\u5e38\u89c1 SP \u5361\u987f&#xff1f; \u5b83\u63d0\u4f9b\u6302\u8d77\u5f0f\u5199\u5165\u3001Flow \u8bfb\u53d6\u4e0e\u4e32\u884c\u4e8b\u52a1\u66f4\u65b0&#xff0c;\u8c03\u7528\u65b9\u4e0d\u9700\u8981\u5728\u4e3b\u7ebf\u7a0b\u540c\u6b65\u7b49\u5f85\u6574\u4efd\u8bbe\u7f6e\u5199\u76d8&#xff1b;\u4f46\u672c\u8eab\u4ecd\u4f1a\u505a\u6587\u4ef6 I\/O \u548c\u5e8f\u5217\u5316\u3002<\/p>\n<p>Q3&#xff1a;Preferences DataStore \u4e0e Proto DataStore \u7684\u533a\u522b&#xff1f; \u524d\u8005\u6309 key \u5b58\u8bbe\u7f6e&#xff0c;\u540e\u8005\u6309 protobuf schema \u5b58\u7c7b\u578b\u5316\u5bf9\u8c61&#xff1b;\u4e24\u8005\u90fd\u7528 DataStore \u7684\u66f4\u65b0\u4e0e\u89c2\u5bdf\u673a\u5236\u3002<\/p>\n<p>Q4&#xff1a;MMKV \u4e3a\u4ec0\u4e48\u5e38\u5728\u5c0f\u578b\u952e\u503c\u8bfb\u5199\u4e2d\u8868\u73b0\u5feb&#xff1f; mmap \u51cf\u5c11 I\/O \u8def\u5f84\u5f00\u9500&#xff0c;\u4e8c\u8fdb\u5236\u7f16\u7801\u4e0e\u8ffd\u52a0\u5199\u907f\u514d\u6bcf\u6b21\u90fd\u628a XML \u5168\u91cf\u91cd\u5199&#xff1b;\u5177\u4f53\u8868\u73b0\u4ecd\u53d7\u6570\u636e\u91cf\u3001\u6574\u7406\u548c\u6301\u4e45\u5316\u8981\u6c42\u5f71\u54cd\u3002<\/p>\n<p>Q5&#xff1a;MMKV \u7684 mmap \u662f\u5426\u7b49\u4e8e\u4e0d\u4f1a\u4e22\u6570\u636e&#xff1f; \u4e0d\u662f\u3002\u6620\u5c04\u5199\u5165\u4e0e\u7269\u7406\u4ecb\u8d28\u843d\u76d8\u4e0d\u662f\u540c\u4e00\u65f6\u523b&#xff1b;\u8981\u533a\u5206 API \u8fd4\u56de\u3001\u540c\u6b65\u5237\u76d8\u548c\u7a81\u7136\u65ad\u7535\u7b49\u573a\u666f\u3002<\/p>\n<p>Q6&#xff1a;\u4e09\u8005\u90fd\u652f\u6301\u591a\u8fdb\u7a0b\u5417&#xff1f; SP \u65e7\u591a\u8fdb\u7a0b\u6a21\u5f0f\u4e0d\u53ef\u9760&#xff1b;DataStore \u9700\u8981\u663e\u5f0f\u591a\u8fdb\u7a0b\u914d\u7f6e&#xff1b;MMKV \u4e5f\u9700\u8981\u663e\u5f0f\u591a\u8fdb\u7a0b\u6a21\u5f0f\u3002\u5373\u4fbf\u652f\u6301&#xff0c;\u591a key \u4e1a\u52a1\u4e00\u81f4\u6027\u4ecd\u8981\u5355\u72ec\u8bbe\u8ba1\u3002<\/p>\n<p>Q7&#xff1a;\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u7528\u5b83\u4eec\u66ff\u4ee3 Room&#xff1f; \u5b83\u4eec\u9762\u5411\u5c0f\u578b\u952e\u503c\u6216\u914d\u7f6e&#xff0c;\u4e0d\u64c5\u957f\u590d\u6742\u67e5\u8be2\u3001\u7d22\u5f15\u3001\u5173\u8054\u53ca\u5927\u91cf\u8bb0\u5f55\u4e8b\u52a1\u3002<\/p>\n<p>\u4e00\u53e5\u8bdd\u6536\u675f&#xff1a;**SP \u662f\u5e73\u53f0\u9057\u7559\u4e14\u591f\u7528\u7684\u7b80\u5355\u8bbe\u7f6e\u65b9\u6848&#xff0c;DataStore \u66f4\u770b\u91cd\u5f02\u6b65\u3001\u4e00\u81f4\u66f4\u65b0\u4e0e\u54cd\u5e94\u5f0f\u89c2\u5bdf&#xff0c;MMKV \u66f4\u770b\u91cd\u5c0f\u578b\u952e\u503c\u8bfb\u5199\u8def\u5f84\u7684\u6548\u7387\u3002**\u5148\u786e\u8ba4\u6570\u636e\u8bed\u4e49\u548c\u53ef\u9760\u6027\u8981\u6c42&#xff0c;\u518d\u8c08\u57fa\u51c6\u6d4b\u8bd5\u91cc\u7684\u6beb\u79d2\u6570\u3002<\/p>\n<h3>\u53c2\u8003\u8d44\u6599<\/h3>\n<ul>\n<li>Android Developers&#xff1a;DataStore<\/li>\n<li>Android API&#xff1a;SharedPreferences<\/li>\n<li>Android API&#xff1a;SharedPreferences.Editor<\/li>\n<li>\u817e\u8baf MMKV \u5f00\u6e90\u9879\u76ee<\/li>\n<li>\u817e\u8baf MMKV Wiki<\/li>\n<li>\u672c\u9879\u76ee&#xff1a;Android \u6570\u636e\u5e93\u4f7f\u7528\u6307\u5357<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u6bd4 SharedPreferences \u7684 XML \u4e0e apply\/commit\u3001DataStore \u7684 Flow \u548c\u4e8b\u52a1\u66f4\u65b0\u3001MMKV \u7684 mmap \u4e0e\u8ffd\u52a0\u5199&#xff0c;\u8bb2\u6e05\u591a\u8fdb\u7a0b\u3001\u6027\u80fd\u3001\u6301\u4e45\u6027\u548c\u8fc1\u79fb\u8fb9\u754c\u3002 \u4e00\u4e2a\u6df1\u8272\u6a21\u5f0f\u5f00\u5173&#xff0c;SharedPreferences\u3001DataStore\u3001MMKV \u90fd\u80fd\u5b58&#xff1b;\u4f46\u5b83\u4eec\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u5f0f\u5e76\u4e0d\u76f8\u540c\u3002SharedPreferences&#xff08;\u4e0b\u6587\u7b80\u79f0 SP&#xff09;\u662f Android \u5e73\u53f0\u81ea\u5e26\u7684 X<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[84],"topic":[],"class_list":["post-115288","post","type-post","status-publish","format-standard","hentry","category-server","tag-android"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android DataStore\u3001SharedPreferences \u4e0e MMKV - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/115288.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android DataStore\u3001SharedPreferences \u4e0e MMKV - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5bf9\u6bd4 SharedPreferences \u7684 XML \u4e0e apply\/commit\u3001DataStore \u7684 Flow \u548c\u4e8b\u52a1\u66f4\u65b0\u3001MMKV \u7684 mmap \u4e0e\u8ffd\u52a0\u5199&#xff0c;\u8bb2\u6e05\u591a\u8fdb\u7a0b\u3001\u6027\u80fd\u3001\u6301\u4e45\u6027\u548c\u8fc1\u79fb\u8fb9\u754c\u3002 \u4e00\u4e2a\u6df1\u8272\u6a21\u5f0f\u5f00\u5173&#xff0c;SharedPreferences\u3001DataStore\u3001MMKV \u90fd\u80fd\u5b58&#xff1b;\u4f46\u5b83\u4eec\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u5f0f\u5e76\u4e0d\u76f8\u540c\u3002SharedPreferences&#xff08;\u4e0b\u6587\u7b80\u79f0 SP&#xff09;\u662f Android \u5e73\u53f0\u81ea\u5e26\u7684 X\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/115288.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-11T12:25:26+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/115288.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/115288.html\",\"name\":\"Android DataStore\u3001SharedPreferences \u4e0e MMKV - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-11T12:25:26+00:00\",\"dateModified\":\"2026-10-11T12:25:26+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/115288.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/115288.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/115288.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android DataStore\u3001SharedPreferences \u4e0e MMKV\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android DataStore\u3001SharedPreferences \u4e0e MMKV - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/115288.html","og_locale":"zh_CN","og_type":"article","og_title":"Android DataStore\u3001SharedPreferences \u4e0e MMKV - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5bf9\u6bd4 SharedPreferences \u7684 XML \u4e0e apply\/commit\u3001DataStore \u7684 Flow \u548c\u4e8b\u52a1\u66f4\u65b0\u3001MMKV \u7684 mmap \u4e0e\u8ffd\u52a0\u5199&#xff0c;\u8bb2\u6e05\u591a\u8fdb\u7a0b\u3001\u6027\u80fd\u3001\u6301\u4e45\u6027\u548c\u8fc1\u79fb\u8fb9\u754c\u3002 \u4e00\u4e2a\u6df1\u8272\u6a21\u5f0f\u5f00\u5173&#xff0c;SharedPreferences\u3001DataStore\u3001MMKV \u90fd\u80fd\u5b58&#xff1b;\u4f46\u5b83\u4eec\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u5f0f\u5e76\u4e0d\u76f8\u540c\u3002SharedPreferences&#xff08;\u4e0b\u6587\u7b80\u79f0 SP&#xff09;\u662f Android \u5e73\u53f0\u81ea\u5e26\u7684 X","og_url":"https:\/\/www.wsisp.com\/helps\/115288.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-11T12:25:26+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/115288.html","url":"https:\/\/www.wsisp.com\/helps\/115288.html","name":"Android DataStore\u3001SharedPreferences \u4e0e MMKV - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-11T12:25:26+00:00","dateModified":"2026-10-11T12:25:26+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/115288.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/115288.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/115288.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Android DataStore\u3001SharedPreferences \u4e0e MMKV"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/115288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=115288"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/115288\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=115288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=115288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=115288"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=115288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}