保存与恢复列表
给模型字段标上 #[persist] 并安装 PersistencePlugin 后,模型在注册时会自动从存储后端读取上次保存的记录,并在字段修改后按间隔自动写入。
1. 最小演示:#[persist] 与模拟重开
先看一个只有十几行的持久化计数器:SavedCount 标上 #[model(persistence(key = "..."))],value 字段标上 #[persist]。
点几下 + 1 (Autosaves),然后点击工作台右下角的 模拟重开 ↻ 按钮(它会销毁当前 App 实例并重新创建),可以看到计数值从存储快照里恢复了:
537#[model(persistence(key = "mirui_mini_saved_count"))]
538pub struct SavedCount {
539 #[observe]
540 #[persist]
541 pub value: i32,
542}
543
544impl Default for SavedCount {
545 fn default() -> Self {
546 Self { value: 1 }
547 }
548}
549
550#[model]
551impl SavedCount {
552 pub fn add(&mut self, delta: i32) {
553 self.value += delta;
554 }
555
556 pub fn reset(&mut self) {
557 self.value = 1;
558 }
559}
560
561#[compose]
562fn mini_storage_scene(counter: model!(SavedCount)) {
563 ui! {
564 Column (
565 grow: 1.0,
566 padding: Padding::all(20),
567 justify: JustifyContent::SpaceBetween,
568 text_color: ColorToken::OnSurface
569 ) {
570 Row (height: 28, justify: JustifyContent::SpaceBetween, align: AlignItems::Center) {
571 Text (
572 "#[persist] snapshot",
573 grow: 1.0,
574 height: 20,
575 font_size: 13,
576 text_color: ColorToken::OnSurfaceVariant,
577 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
578 )
579 Text (
580 text: ${ format_args!("Saved: {}", counter.value()) },
581 capacity: 20,
582 grow: 1.0,
583 font_size: 20,
584 height: 28,
585 paragraph: ParagraphStyle::label().with_align(TextAlign::End)
586 )
587 }
588 Row (height: 40, column_gap: 8) {
589 Button (
590 "+ 1 (Autosaves)",
591 grow: 1.0,
592 height: 40,
593 border_radius: 9,
594 normal_color: ColorToken::Primary,
595 pressed_color: ColorToken::Secondary,
596 text_color: ColorToken::OnPrimary
597 ) on Tap { counter.add(1); }
598 Button (
599 "Reset",
600 width: 80,
601 height: 40,
602 border_radius: 9,
603 normal_color: ColorToken::SurfaceVariant,
604 pressed_color: ColorToken::Outline,
605 text_color: ColorToken::OnSurface
606 ) on Tap { counter.reset(); }
607 }
608 }
609 };
610}
正在加载预览…
2. 带入任务清单:自动保存与启动恢复
现在把持久化加进任务清单。在 model.rs 里给 Task 和 Tasks 的业务字段标上 #[persist],并在 lib.rs 里确保 PersistencePlugin 在 app.add_model(...) 之前安装:
CHAPTER 06 · PERSISTENCE & PLUGIN ORDER UMLPersistencePlugin 注册时序与 #[persist] vs #[observe] 选择性落盘关系图
`lib.rs` 必须在调用 `app.add_model(Tasks::default()).finish()` **之前**安装 `PersistencePlugin`,否则模型注册时找不到存储服务。
app.add_plugin(PersistencePlugin::new(storage).autosave(500));Plugin order contract enforced⋯model.rs:7
+7#[model(persistence)]
8#[derive(Default)]
9pub struct Task {
+10 #[persist]
11 pub title: String,
12 #[observe]
+13 #[persist]
14 pub done: bool,
15}
16
17#[model]
18impl Task {
19 pub fn toggle(&mut self) {
20 self.done = !self.done;
21 }
22}
⋯model.rs:41
+41#[model(persistence(key = "first_app_tasks"))]
42pub struct Tasks {
+43 #[persist]
44 pub items: model!(owned ModelList<Task>),
45 #[observe]
46 pub(crate) filter: Filter,
+47 #[persist]
48 sequence: u32,
49}
⋯lib.rs:69
+69 if chapter.has_storage()
+70 && let Some(storage) = storage
+71 {
+72 app.add_plugin(PersistencePlugin::new(storage).autosave(500));
+73 }
⋯lib.rs:85
85 let tasks = app.add_model(Tasks::default()).finish();
正在加载预览…
这一步做了什么
- 先装插件,再注册模型:
app.add_model(Tasks::default()).finish()内部会立即向已安装的PersistencePlugin查询是否有旧快照。因此在lib.rs里必须先把插件挂好,第一帧画出来的才不会闪烁默认值。 - 只存业务数据,不存临时视图状态:注意
Tasks里的filter只标了#[observe]而没有标#[persist]。上次停在哪个筛选标签、或者滑到一半的动画属于临时视图状态,重开时干净地回到Filter::All,而任务列表与完成数则完整还原。
在本机运行
sh
cargo run -p mirui-first-app --features desktop --example desktop -- 6