2

So I finally got my listview content to serialize and write to a file so I can restore my apps state across different sessions. Now I'm wondering if there is a way I can incrementally serialize and save my data. Currently, I call this method in the SaveState method of my mainpage:

    private async void writeToFile()
    {
        var f = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("data.txt", CreationCollisionOption.ReplaceExisting);
        using (var st = await f.OpenStreamForWriteAsync())
        {
            var s = new DataContractSerializer(typeof(ObservableCollection<Item>),
                                   new Type[] { typeof(Item) });
            s.WriteObject(st, convoStrings);
        }
    }

What I think would be more ideal is to write out data to storage as it is generated, so I don't have to serialize my entire list in the small suspend time frame, but I don't know if this is possible to incrementally serialize my collection, and if it is, how would I do it?

Note that my data doesn't change after it is generated, so I don't have to worry about anything other than appending new data to the end of my currently serialized list.

4

1 回答 1

0

这取决于您定义何时将数据保存到硬盘驱动器。也许您想在添加或删除新的集合项时保存新的集合状态?或者当项目的内容发生变化时。

将所有内容及时保存到硬盘的主要问题是,这可能会很慢。如果您使用的是async编程模型,这不会直接成为问题,因为您的应用程序不会挂起,因为是的,一切都是异步的。

我认为每分钟和用户关闭应用程序时保存集合可能是一个更好的主意。这仅在您处理一定数量的数据时才有效,因为您只有大约 3 秒的时间来执行所有 IO 工作。

如您所见,没有完美的解决方案。这实际上取决于您的要求和数据的大小。没有更多信息,我可以肯定地告诉你。

于 2012-12-21T22:31:02.430 回答