简体   繁体   English

孤立存储文件的困难

[英]Difficulty with IsolatedStorageFile

I'm building a Windows Phone 7 app in Silverlight. 我正在Silverlight中构建Windows Phone 7应用程序。 I'm having difficulty using IsolatedStorageFile . 我在使用IsolatedStorageFile遇到困难。

The following method is supposed to write some data to a file: 应该使用以下方法将一些数据写入文件:

    private static void writeToFile(IList<Story> stories)
    {
        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
        using (IsolatedStorageFileStream stream = storage.OpenFile(STORIES_FILE, FileMode.Append))
        {
            using (StreamWriter writer = new StreamWriter(stream))
            {
                StringBuilder toJson = new StringBuilder();

                IList<StoryJson> storyJsons = (from story in stories
                                               where !storageStories.Contains(story)
                                               select story.ToStoryJson()).ToList();

                writer.Write(JsonConvert.SerializeObject(storyJsons));
            }

        }

#if DEBUG
            StreamReader reader = new StreamReader(storage.OpenFile(STORIES_FILE, FileMode.Open));
            string contents = reader.ReadToEnd();
#endif
        }

The DEBUG at the end is for me to check that the data is actually being written. 最后的DEBUG对我来说是检查数据是否正在实际写入中。 I have verified that it is. 我已经证实了。 This method is called 6+ times. 此方法称为6次以上。 Each time, more data is appended. 每次附加更多数据。

However, when I go to read the data, the only JSON I get back is that which I wrote in one call of writeToFile() . 但是,当我去读取数据时,我得到的唯一JSON是我在一次 writeToFile()调用中writeToFile() Here is my method to read: 这是我的阅读方法:

    private static IList<Story> storageStories;
    private static IList<Story> readFromStorage()
    {
        if (storageStories != null)
        {
            return storageStories;
        }

        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();

        if (! storage.FileExists(STORIES_FILE))
        {
            storage.CreateFile(STORIES_FILE);
            storageStories = new List<Story>();
            return storageStories;
        }

        string contents;
        using (IsolatedStorageFileStream stream = storage.OpenFile(STORIES_FILE, FileMode.OpenOrCreate))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                contents = reader.ReadToEnd();
            }
        }

        JsonSerializer serializer = new JsonSerializer();
        storageStories = JArray.Parse(contents).Select(storyData => storyOfJson(serializer, storyData)).ToList();
        return storageStories;
    }

What could I be doing wrong here? 我在这里可能做错了什么? Am I writing to the file incorrectly? 我写的文件不正确吗? I'm pretty sure that the only data that is able to be read back is from the first write. 我很确定,唯一可以读取的数据是第一次写入。

Update : I added two Flush() calls, but it crashes: 更新 :我添加了两个Flush()调用,但是崩溃了:

  private static void writeToFile(IList<Story> stories)
        {
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
            using (IsolatedStorageFileStream stream = storage.OpenFile(STORIES_FILE, FileMode.Append))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    StringBuilder toJson = new StringBuilder();

                    IList<StoryJson> storyJsons = (from story in stories
                                                   where !storageStories.Contains(story)
                                                   select story.ToStoryJson()).ToList();

                    writer.Write(JsonConvert.SerializeObject(storyJsons));
                    writer.Flush();
                }
                // FAILS
                // "Cannot access a closed file." {System.ObjectDisposedException}

                stream.Flush();
            }
        }

If I comment out the stream.Flush() but leave writer.Flush() , I have the same problem. 如果我注释掉stream.Flush()但离开writer.Flush() ,我也有同样的问题。

Update 2 : I added some print statements. 更新2 :我添加了一些打印语句。 Looks like everything is getting serialized: 看起来一切都已序列化:

Serializing for VID 43
Serializing for VID 17
Serializing for VID 6
Serializing for VID 33
Serializing for VID 4
Serializing for VID 5
Serializing for VID 3

But only the first set is actually being read back: 但是实际上只有第一组被读回:

Deserializing stories with vid: 43

I have run the test a few more times. 我已经进行了几次测试。 I'm pretty sure that only the first item is ever being read back. 我敢肯定,只有第一项才能被读回。

At first glance it sounds like your stream data is not being flushed to disk. 乍一看,听起来好像您的流数据没有被刷新到磁盘上。

You are probably thinking that the using block will perform a flush when it Disposes the stream. 你可能在想的是, using时,块将执行冲洗Disposes流。 However I have found that is not always the case and sometimes it is best to force a Flush() at the end. 但是我发现情况并非总是如此, 有时最好在最后强制使用Flush()

I remember recently in a codebase that we received from a Microsoft team to port to WP7 that they were forcing a Flush . 我记得最近在我们从Microsoft团队收到的移植到WP7的代码库中,他们迫使Flush I questioned it initially, thinking that the Dispose should handle that, however as it was working and we were on a short deadline I did not investigate it further. 我最初质疑它,认为是Dispose应该处理该问题,但是由于它正在运行,而且我们的截止日期很短,因此我不会对其进行进一步调查。

Give it go, see what happens... :) 放手,看看会发生什么... :)

Have you tried explicitly calling 您是否尝试过明确呼叫

writer.Close()

rather than relying on writer.Dispose() 而不是依赖writer.Dispose()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM