階之道(28):PersistentStorage 持久化狀態(tài)邊界——為啥應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài)根因)
ArkTS 進(jìn)階之道28PersistentStorage 持久化狀態(tài)邊界——為啥應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài)根因本文是「ArkTS 進(jìn)階之道」系列第 28 篇續(xù)「ArkUI 狀態(tài)聯(lián)動(dòng)」深水區(qū)。上篇講狀態(tài)管理裝飾器全景邊界V1/V2 雙軌根因。本文講持久化狀態(tài)邊界PersistentStorage 持久化 AppStorage 鍵到磁盤——根因在應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài)。能力系列篇 28 講過(guò) PersistentStorage 怎么用本文講為哈 PersistentStorage 持久化狀態(tài)合法 AppStorage 應(yīng)用級(jí)狀態(tài)頁(yè)面卸載狀態(tài)仍存在不跨啟動(dòng)——根因在持久化狀態(tài)存儲(chǔ)槽。一、開篇PersistentStorage 不是應(yīng)用級(jí) AppStorage是持久化到磁盤的跨啟動(dòng)狀態(tài)你寫 React 時(shí)持久化狀態(tài)是「localStorage 魔法」localStorage JSON 序列化到磁盤// React 持久化狀態(tài)localStorage 魔法JSON 序列化到磁盤 function usePersistedState(key: string, defaultValue: any) { const [value, setValue] useState(() { const stored localStorage.getItem(key) // 從磁盤讀 return stored ? JSON.parse(stored) : defaultValue }) useEffect(() { localStorage.setItem(key, JSON.stringify(value)) }, [key, value]) // 改寫磁盤 return [value, setValue] } // React 持久化狀態(tài)localStorage 魔法JSON 序列化到磁盤應(yīng)用重啟仍存在你寫鴻蒙 ArkTS 時(shí)PersistentStorage持久化 AppStorage 鍵到磁盤——應(yīng)用重啟后狀態(tài)仍存在// ArkTS PersistentStorage 持久化 AppStorage 鍵到磁盤 AppStorage.setOrCreate(persistCount, 0) PersistentStorage.persistProp(persistCount, 0) // ? 持久化 AppStorage 鍵到磁盤 Entry Component struct Index { StorageLink(persistCount) persistCount: number 0 // ? StorageLink 雙向綁定持久化鍵 build() { Button(persistCount${this.persistCount}) .onClick(() { this.persistCount }) // ? 改 → AppStorage 同步 → PersistentStorage 持久化到磁盤 } } // PersistentStorage 持久化 AppStorage 鍵到磁盤應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài)localStorage 魔法 vs 持久化狀態(tài)存儲(chǔ)槽的區(qū)別React 把持久化狀態(tài)當(dāng) localStorage 魔法JSON 序列化到磁盤ArkTS 把 PersistentStorage 當(dāng)「持久化狀態(tài)存儲(chǔ)槽」持久化 AppStorage 鍵到磁盤。根因不是 localStorage 魔法是持久化狀態(tài)存儲(chǔ)槽——PersistentStorage 持久化 AppStorage 鍵到磁盤應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài)。二、根因PersistentStorage 的持久化狀態(tài)存儲(chǔ)槽機(jī)制鴻蒙 ArkUI 的 PersistentStorage 是持久化狀態(tài)存儲(chǔ)槽綁定——持久化 AppStorage 鍵到磁盤應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài)來(lái)自三重綁定機(jī)制。機(jī)制 1PersistentStorage 持久化 AppStorage 鍵到磁盤——不是應(yīng)用級(jí)內(nèi)存PersistentStorage 荬飾器持久化 AppStorage 鍵到磁盤——應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài)// ? 初始化 PersistentStorage持久化 AppStorage 鍵到磁盤 AppStorage.setOrCreate(persistCount, 0) PersistentStorage.persistProp(persistCount, 0) // ? 持久化 AppStorage 鍵到磁盤 Entry Component struct Index { StorageLink(persistCount) persistCount: number 0 // ? StorageLink 雙向綁定持久化鍵 build() { Column() { Text(persistCount ${this.persistCount}) Button(改 persistCount) .onClick(() { this.persistCount }) // ? 改 → AppStorage 同步 → PersistentStorage 持久化到磁盤 } } } // PersistentStorage 持久化 AppStorage 鍵到磁盤應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài)不丟持久化 AppStorage 鍵到磁盤PersistentStoragepersistCount持久化 AppStorage 鍵到磁盤——應(yīng)用重啟后persistCount仍存在跨啟動(dòng)狀態(tài)不丟。根因不是應(yīng)用級(jí)內(nèi)存是持久化狀態(tài)存儲(chǔ)槽——PersistentStorage 持久化 AppStorage 鍵到磁盤應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài)。機(jī)制 2StorageLink 雙向綁定持久化鍵——改互同步持久化到磁盤StorageLink 荬飾器雙向綁定持久化鍵——改 StorageLink → AppStorage 同步 → PersistentStorage 持久化到磁盤Entry Component struct Index { StorageLink(persistCount) persistCount: number 0 // ? StorageLink 雙向綁定持久化鍵 build() { Column() { Text(persistCount ${this.persistCount}) Button(改 persistCount) .onClick(() { this.persistCount }) // ? 改 → AppStorage 同步 → PersistentStorage 持久化到磁盤 } } } // StorageLink 雙向綁定持久化鍵改 → AppStorage 同步 → PersistentStorage 持久化到磁盤跨啟動(dòng)StorageLink 雙向綁定持久化鍵StorageLinkpersistCount荬飾器雙向綁定持久化鍵——改persistCount→ AppStorage 同步 → PersistentStorage 持久化到磁盤跨啟動(dòng)。根因不是只 AppStorage 同步是持久化到磁盤——StorageLink 雙向綁定持久化鍵改 → AppStorage 同步 → PersistentStorage 持久化到磁盤。機(jī)制 3AppStorage.set 改 → PersistentStorage 同步持久化——不用 StorageLink 也能持久化AppStorage.set改 AppStorage 鍵→ PersistentStorage 同步持久化到磁盤不用 StorageLink 也能持久化Entry Component struct Index { StorageLink(persistCount) persistCount: number 0 // ? StorageLink 雙向綁定持久化鍵 build() { Column() { Text(persistCount ${this.persistCount}) Button(直接改 AppStorage.set(persistCount, 999)) .onClick(() { AppStorage.set(persistCount, 999) // ? AppStorage 改 → PersistentStorage 同步持久化到磁盤 }) } } } // AppStorage.set 改 → PersistentStorage 同步持久化不用 StorageLink 也能持久化到磁盤AppStorage.set 改 → PersistentStorage 同步持久化AppStorage.setpersistCount改 AppStorage 鍵 → PersistentStorage 同步持久化到磁盤不用 StorageLink 也能持久化。根因不是必須用 StorageLink 是 AppStorage.set 直接改——AppStorage.set 改 → PersistentStorage 同步持久化到磁盤。機(jī)制 4PersistentStorage vs AppStorage 邊界——跨啟動(dòng) vs 應(yīng)用級(jí)PersistentStorage持久化狀態(tài)應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)AppStorage應(yīng)用級(jí)狀態(tài)所有頁(yè)面共享頁(yè)面卸載狀態(tài)仍存在不跨啟動(dòng)——根因都是狀態(tài)存儲(chǔ)但綁的機(jī)制不同// ? PersistentStorage 路徑持久化狀態(tài)應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng) AppStorage.setOrCreate(persistCount, 0) PersistentStorage.persistProp(persistCount, 0) // ? 持久化到磁盤 Entry Component struct PersistPage { StorageLink(persistCount) persistCount: number 0 // ? 持久化鍵跨啟動(dòng) build() { Button(持久化 ${this.persistCount}).onClick(() { this.persistCount }) } } // ? PersistentStorage 持久化狀態(tài)應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)持久化到磁盤 // ? AppStorage 路徑應(yīng)用級(jí)狀態(tài)所有頁(yè)面共享頁(yè)面卸載狀態(tài)仍存在不跨啟動(dòng) AppStorage.setOrCreate(appCount, 0) Entry Component struct AppPage { StorageLink(appCount) appCount: number 0 // ? 應(yīng)用級(jí)鍵不跨啟動(dòng)應(yīng)用重啟銷毀 build() { Button(應(yīng)用級(jí) ${this.appCount}).onClick(() { this.appCount }) } } // ? AppStorage 應(yīng)用級(jí)狀態(tài)所有頁(yè)面共享頁(yè)面卸載狀態(tài)仍存在不跨啟動(dòng)內(nèi)存應(yīng)用重啟銷毀PersistentStorage vs AppStorage 邊界PersistentStorage 路徑——持久化狀態(tài)應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)持久化到磁盤。AppStorage 路徑——應(yīng)用級(jí)狀態(tài)所有頁(yè)面共享頁(yè)面卸載狀態(tài)仍存在不跨啟動(dòng)內(nèi)存應(yīng)用重啟銷毀。根因都是狀態(tài)存儲(chǔ)但綁的機(jī)制不同——PersistentStorage 持久化狀態(tài)存儲(chǔ)槽跨啟動(dòng)AppStorage 應(yīng)用級(jí)狀態(tài)存儲(chǔ)槽不跨啟動(dòng)。三、真機(jī)配圖PersistentStorage 持久化狀態(tài)邊界——跨啟動(dòng)狀態(tài)初始態(tài)persistCount0 葝色、持久化狀態(tài)邊界初始態(tài)齊點(diǎn)調(diào)兩按鈕后persistCount999 葝色被 AppStorage.set 直接改 999 覆蓋、PersistentStorage 持久化狀態(tài)邊界證據(jù)齊對(duì)比證據(jù)點(diǎn)改 persistCount 后 AppStorage 同步 PersistentStorage 持久化到磁盤StorageLink 雙向同步點(diǎn)直接改 AppStorage.set(“persistCount”, 999) 后 persistCount999 被 AppStorage.set 直接改 999 覆蓋 PersistentStorage 同步持久化到磁盤。PersistentStorage 持久化狀態(tài)——持久化 AppStorage 鍵到磁盤應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài)StorageLink 雙向綁定持久化鍵AppStorage.set 改 → PersistentStorage 同步持久化。四、真解法PersistentStorage 持久化狀態(tài)的三個(gè)場(chǎng)景場(chǎng)景 1PersistentStorage 持久化用戶偏好90% 場(chǎng)景首選跨啟動(dòng)狀態(tài)用持久化AppStorage.setOrCreate(themeColor, #007DFF) PersistentStorage.persistProp(themeColor, #007DFF) // ? 持久化用戶偏好到磁盤 Entry Component struct Index { StorageLink(themeColor) themeColor: string #007DFF // ? StorageLink 雙向綁定持久化鍵 build() { Column() { Text(themeColor ${this.themeColor}).fontColor(this.themeColor) Button(改 themeColor紅色) .onClick(() { this.themeColor #FF4D4F }) // ? 改 → AppStorage 同步 → PersistentStorage 持久化到磁盤 } } }為哈能跑PersistentStorage 持久化用戶偏好——持久化狀態(tài)存儲(chǔ)槽PersistentStoragethemeColor持久化 AppStorage 鍵到磁盤StorageLink 雙向綁定持久化鍵改themeColor→ AppStorage 同步 → PersistentStorage 持久化到磁盤跨啟動(dòng)。首選這個(gè)90% 的場(chǎng)景跨啟動(dòng)狀態(tài)用 PersistentStorage 持久化狀態(tài)存儲(chǔ)槽就夠。要寫「跨啟動(dòng)狀態(tài)用戶偏好等」時(shí)用這個(gè)——不用 AppStorage 應(yīng)用級(jí)不跨啟動(dòng)PersistentStorage 持久化到磁盤跨啟動(dòng)。場(chǎng)景 2PersistentStorage 持久化登錄狀態(tài)跨啟動(dòng)狀態(tài)應(yīng)用重啟仍登錄AppStorage.setOrCreate(isLoggedIn, false) PersistentStorage.persistProp(isLoggedIn, false) // ? 持久化登錄狀態(tài)到磁盤 Entry Component struct Index { StorageLink(isLoggedIn) isLoggedIn: boolean false // ? StorageLink 雙向綁定持久化鍵 build() { Column() { Text(isLoggedIn ${this.isLoggedIn}) Button(登錄) .onClick(() { this.isLoggedIn true }) // ? 改 → AppStorage 同步 → PersistentStorage 持久化到磁盤 Button(退出) .onClick(() { this.isLoggedIn false }) // ? 改 → AppStorage 同步 → PersistentStorage 持久化到磁盤 } } } // PersistentStorage 持久化登錄狀態(tài)應(yīng)用重啟仍登錄跨啟動(dòng)狀態(tài)持久化到磁盤為哈能跑PersistentStorage 持久化登錄狀態(tài)——持久化狀態(tài)存儲(chǔ)槽PersistentStorageisLoggedIn持久化 AppStorage 鍵到磁盤StorageLink 雙向綁定持久化鍵改isLoggedIn→ AppStorage 同步 → PersistentStorage 持久化到磁盤跨啟動(dòng)。要寫「跨啟動(dòng)狀態(tài)登錄狀態(tài)等」時(shí)用這個(gè)——不用 AppStorage 應(yīng)用級(jí)不跨啟動(dòng)PersistentStorage 持久化到磁盤跨啟動(dòng)應(yīng)用重啟仍登錄。場(chǎng)景 3PersistentStorage vs AppStorage vs LocalStorage 作用域邊界跨啟動(dòng) vs 應(yīng)用級(jí) vs 頁(yè)面級(jí)// ? PersistentStorage 跨啟動(dòng)應(yīng)用重啟后狀態(tài)仍存在持久化到磁盤跨啟動(dòng)狀態(tài)用這個(gè) AppStorage.setOrCreate(persistState, 0) PersistentStorage.persistProp(persistState, 0) // ? 跨啟動(dòng)持久化到磁盤 Entry Component struct PersistComp { StorageLink(persistState) persistState: number 0 // ? 跨啟動(dòng)應(yīng)用重啟仍存在 build() { Button(跨啟動(dòng) ${this.persistState}).onClick(() { this.persistState }) } } // ? AppStorage 應(yīng)用級(jí)所有頁(yè)面共享頁(yè)面卸載狀態(tài)仍存在不跨啟動(dòng)內(nèi)存應(yīng)用級(jí)狀態(tài)用這個(gè) AppStorage.setOrCreate(appState, 0) Entry Component struct AppComp { StorageLink(appState) appState: number 0 // ? 應(yīng)用級(jí)所有頁(yè)面共享不跨啟動(dòng) build() { Button(應(yīng)用級(jí) ${this.appState}).onClick(() { this.appState }) } } // ? LocalStorage 頁(yè)面級(jí)同頁(yè)面多組件共享頁(yè)面卸載就銷毀內(nèi)存頁(yè)面級(jí)狀態(tài)用這個(gè) interface LocalState { pageState: number } const localStorage new LocalStorage({ pageState: 0 } as LocalState) Component struct PageComp { LocalStorageLink(pageState) pageState: number 0 // ? 頁(yè)面級(jí)同頁(yè)面共享頁(yè)面卸載就銷毀 build() { Button(頁(yè)面級(jí) ${this.pageState}).onClick(() { this.pageState }) } } // PersistentStorage vs AppStorage vs LocalStorage 作用域邊界 // 跨啟動(dòng)用 PersistentStorage持久化到磁盤應(yīng)用級(jí)用 AppStorage內(nèi)存不跨啟動(dòng)頁(yè)面級(jí)用 LocalStorage內(nèi)存頁(yè)面卸載銷毀為哈能跑PersistentStorage vs AppStorage vs LocalStorage 作用域邊界——跨啟動(dòng)狀態(tài)用 PersistentStorage持久化到磁盤應(yīng)用重啟仍存在應(yīng)用級(jí)狀態(tài)用 AppStorage內(nèi)存不跨啟動(dòng)所有頁(yè)面共享頁(yè)面級(jí)狀態(tài)用 LocalStorage內(nèi)存頁(yè)面卸載銷毀同頁(yè)面多組件共享。要寫「選對(duì)狀態(tài)存儲(chǔ)槽作用域跨啟動(dòng) vs 應(yīng)用級(jí) vs 頁(yè)面級(jí)等」時(shí)用這個(gè)——跨啟動(dòng)用 PersistentStorage應(yīng)用級(jí)用 AppStorage頁(yè)面級(jí)用 LocalStorage。五、一句話哲學(xué)PersistentStorage 不是應(yīng)用級(jí) AppStorage是持久化到磁盤的跨啟動(dòng)狀態(tài)存儲(chǔ)槽。ArkUI 的 PersistentStorage 荬飾器持久化 AppStorage 鍵到磁盤應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài)。根因不是應(yīng)用級(jí) AppStorage 是持久化狀態(tài)存儲(chǔ)槽——PersistentStorage 持久化 AppStorage 鍵到磁盤應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài) StorageLink 雙向綁定持久化鍵改 → AppStorage 同步 → PersistentStorage 持久化到磁盤 AppStorage.set 改 → PersistentStorage 同步持久化不用 StorageLink 也能持久化到磁盤 PersistentStorage vs AppStorage 邊界跨啟動(dòng) vs 應(yīng)用級(jí)。對(duì)比 React 持久化狀態(tài) localStorage 魔法JSON 序列化到磁盤ArkTS PersistentStorage 持久化狀態(tài)存儲(chǔ)槽跨啟動(dòng)狀態(tài)。狀態(tài)聯(lián)動(dòng)深水區(qū)串講Watch 綁 State 變觸發(fā)副作用回調(diào)槽篇 68 Link $ 語(yǔ)法雙向綁定跨組件同步篇 69 Provide/Consume 跨層級(jí)傳遞槽祖輩后輩同步篇 70 Observed/ObjectLink 嵌套對(duì)象觀測(cè)槽子屬性改刷 UI篇 71 AppStorage 應(yīng)用級(jí)狀態(tài)存儲(chǔ)槽所有頁(yè)面共享篇 72 LocalStorage 頁(yè)面級(jí)狀態(tài)存儲(chǔ)槽同頁(yè)面多組件共享篇 73 StorageLink/StorageProp 應(yīng)用級(jí)狀態(tài)綁定邊界雙向 vs 只讀篇 74 LocalStorageLink/LocalStorageProp 頁(yè)面級(jí)狀態(tài)綁定邊界雙向 vs 只讀篇 75 狀態(tài)管理裝飾器全景邊界 V1/V2 雙軌篇 76 PersistentStorage 持久化狀態(tài)存儲(chǔ)槽跨啟動(dòng)狀態(tài)篇 77應(yīng)用重啟后狀態(tài)仍存在——續(xù)「ArkUI 狀態(tài)聯(lián)動(dòng)」深水區(qū)講狀態(tài)聯(lián)動(dòng)深水區(qū)Watch 副作用回調(diào)槽邊界 Link $ 語(yǔ)法雙向綁定邊界 Provide/Consume 跨層級(jí)傳遞槽邊界 Observed/ObjectLink 嵌套對(duì)象觀測(cè)槽邊界 AppStorage 應(yīng)用級(jí)狀態(tài)存儲(chǔ)槽邊界 LocalStorage 頁(yè)面級(jí)狀態(tài)存儲(chǔ)槽邊界 StorageLink/StorageProp 應(yīng)用級(jí)狀態(tài)綁定邊界 LocalStorageLink/LocalStorageProp 頁(yè)面級(jí)狀態(tài)綁定邊界 狀態(tài)管理裝飾器全景邊界 V1/V2 雙軌 PersistentStorage 持久化狀態(tài)存儲(chǔ)槽邊界。系列預(yù)告下篇篇 78講 Environment 環(huán)境變量狀態(tài)邊界環(huán)境變量注入根因續(xù)「ArkUI 狀態(tài)聯(lián)動(dòng)」深水區(qū)。五階段哲學(xué)體系類型哲學(xué)50-52→ 作用域哲學(xué)53-55→ 狀態(tài)哲學(xué)56-59→ 瀆染哲學(xué)60-62→ 組件設(shè)計(jì)63-67→ 狀態(tài)聯(lián)動(dòng)深水區(qū)68講清 ArkTS/ArkUI 進(jìn)階哲學(xué)。能力系列回鏈能力系列篇本文進(jìn)階點(diǎn)篇 28 PersistentStorage 用法PersistentStorage 持久化狀態(tài)存儲(chǔ)槽邊界根因跨啟動(dòng)狀態(tài)持久化到磁盤篇 23 AppStorage 用法應(yīng)用級(jí)狀態(tài)存儲(chǔ)槽邊界所有頁(yè)面共享頁(yè)面卸載狀態(tài)仍存在不跨啟動(dòng)篇 59 Watch 用法狀態(tài)聯(lián)動(dòng)深水區(qū)Watch 副作用回調(diào)槽 vs onClick 直接調(diào)副作用邊界真機(jī) demo 完整代碼// 篇 77 demoPersistentStorage 持久化狀態(tài)邊界 // PersistentStorage 持久化 AppStorage 鍵到磁盤——應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài) AppStorage.setOrCreate(persistCount, 0) PersistentStorage.persistProp(persistCount, 0) // ? 持久化 AppStorage 鍵到磁盤API 21 用 persistProp Entry Component struct Index { StorageLink(persistCount) persistCount: number 0 // ? StorageLink 雙向綁定持久化鍵 State log: string (未操作) build() { Column({ space: 12 }) { Text(篇 77 配圖PersistentStorage 持久化狀態(tài)邊界) .fontSize(16).fontWeight(FontWeight.Bold).margin({ top: 16, bottom: 4 }) Text(PersistentStorage 持久化 AppStorage 鍵到磁盤——應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài)) .fontSize(10).fontColor(#888).margin({ bottom: 8 }) Column({ space: 6 }) { Text(持久化狀態(tài)PersistentStorage) .fontSize(12).fontColor(#2563eb).fontWeight(FontWeight.Bold) Text(persistCount ${this.persistCount}) .fontSize(15).fontWeight(FontWeight.Bold).fontColor(#2563eb) Text(應(yīng)用重啟后仍存在——跨啟動(dòng)狀態(tài)) .fontSize(10).fontColor(#888) } .width(92%).padding(10).backgroundColor(#e0f0ff).borderRadius(8) Button(改 persistCount持久化到磁盤) .width(92%).height(40).fontSize(12) .onClick(() { this.persistCount // ? 改 → AppStorage 同步 → PersistentStorage 持久化到磁盤 this.log 改 persistCount${this.persistCount}持久化到磁盤重啟仍存在 }) Button(直接改 AppStorage.set(persistCount, 999)) .width(92%).height(40).fontSize(12) .onClick(() { AppStorage.set(persistCount, 999) // ? AppStorage 改 → PersistentStorage 同步持久化 this.log 直接改 AppStorage.set persistCount999持久化同步 }) Text(日志${this.log}).fontSize(10).fontColor(#333).margin({ top: 4 }) Text(對(duì)比AppStorage 應(yīng)用級(jí)狀態(tài)頁(yè)面卸載狀態(tài)仍存在vs PersistentStorage 持久化狀態(tài)應(yīng)用重啟仍存在) .fontSize(10).fontColor(#ff6600).margin({ top: 8 }) } .width(100%).height(100%).alignItems(HorizontalAlign.Center) } }寫鴻蒙 ArkUI 記住PersistentStorage 不是應(yīng)用級(jí) AppStorage 是持久化到磁盤的跨啟動(dòng)狀態(tài)存儲(chǔ)槽——PersistentStorage 荬飾器持久化 AppStorage 鍵到磁盤應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài)。根因不是應(yīng)用級(jí) AppStorage 是持久化狀態(tài)存儲(chǔ)槽——PersistentStorage 持久化 AppStorage 鍵到磁盤應(yīng)用重啟后狀態(tài)仍存在跨啟動(dòng)狀態(tài) StorageLink 雙向綁定持久化鍵改 → AppStorage 同步 → PersistentStorage 持久化到磁盤 AppStorage.set 改 → PersistentStorage 同步持久化不用 StorageLink 也能持久化到磁盤 PersistentStorage vs AppStorage 邊界跨啟動(dòng) vs 應(yīng)用級(jí)。PersistentStorage 持久化用戶偏好用持久化狀態(tài)存儲(chǔ)槽首選90% 場(chǎng)景PersistentStorage 持久化登錄狀態(tài)用持久化狀態(tài)存儲(chǔ)槽跨啟動(dòng)應(yīng)用重啟仍登錄PersistentStorage vs AppStorage vs LocalStorage 作用域邊界選對(duì)存儲(chǔ)槽跨啟動(dòng)用 PersistentStorage 持久化到磁盤應(yīng)用級(jí)用 AppStorage 內(nèi)存不跨啟動(dòng)頁(yè)面級(jí)用 LocalStorage 內(nèi)存頁(yè)面卸載銷毀。持久化到磁盤跨啟動(dòng)狀態(tài)應(yīng)用重啟仍存在是 ArkUI 狀態(tài)聯(lián)動(dòng)深水區(qū)核心