0

In wp8.0 we can store object to IsolatedStorageSettings. wp8.1 object was not storing. Is there any way to store object to wp8.1.

WRITE OBJECT CODE

NewsList = new ObservableCollection<New>(e.News);
                var FileName = "News.xml";
                DataContractSerializer serializer = new DataContractSerializer(typeof(ObservableCollection<New>));

                var localFolder = ApplicationData.Current.LocalFolder;
                var file = await localFolder.CreateFileAsync(FileName,CreationCollisionOption.ReplaceExisting);
                IRandomAccessStream sessionRandomAccess = await file.OpenAsync(FileAccessMode.ReadWrite);
                IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
                serializer.WriteObject(sessionOutputStream.AsStreamForWrite(), NewsList);

READ OBJECT CODE

var FileNameNews = "News.xml";
                DataContractSerializer serializer = new DataContractSerializer(typeof(ObservableCollection<New>));
                var localFolder = ApplicationData.Current.LocalFolder;

                var newsFile = await localFolder.GetFileAsync(FileNameNews);

                IInputStream sessionInputStream = await newsFile.OpenReadAsync();
                newsVM = new NewsViewModel();
                NewsVM.NewsList = (ObservableCollection<New>)serializer.ReadObject(sessionInputStream.AsStreamForRead());

im getting error on this link

IInputStream sessionInputStream = await newsFile.OpenReadAsync();

What mistake is there this code??

Thanks

Jeeva123
  • 1,047
  • 3
  • 20
  • 34
  • What error do you get? Have you checked the file if it exists and its content? Finally maybe `NewsVM.NewsList = (ObservableCollection)serializer.ReadObject(await newsFile.OpenStreamForReadAsync());` will work? – Romasz Aug 11 '14 at 10:11
  • getting exception : A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.ni.dll – Jeeva123 Aug 11 '14 at 11:40
  • As I think, there is a chance that you don't dispose the resources (Stream). Put your Streams into `using(Stream = ...)`. See if that helps. You cannot access to a file at the same time from two sources. – Romasz Aug 11 '14 at 11:58
  • how to use?? i'm not getting?? – Jeeva123 Aug 11 '14 at 12:23
  • 1
    [Here](http://stackoverflow.com/q/21849255/2681948) you have an example how to use `using()`. You can use it only for *IDisposable*, once you leave `using(){}` then for example stream is disposed. – Romasz Aug 11 '14 at 12:37
  • its working when i closed stream. thanks for your support. – Jeeva123 Aug 12 '14 at 07:21

1 Answers1

1

This is how I do it. No using statements. I try to avoid the Stream syntax as much as possible.

Your error is very likely either because of concurrency (accessing the same file at the same time will throw an exception), or because the stream was not closed properly. I think it is the latter.

You do not dispose of your Stream objects properly (learn the using () {} syntax), which means that the stream remains OPEN after you're done writing. That means you hit the concurrency issue the second time you write, because you're trying to access a stream that's already open.


    public async Task CreateOrUpdateData(string key, object o)
    {
        try
        {
            if (o != null)
            {
                var sessionFile = await _localFolder.CreateFileAsync(key, CreationCollisionOption.ReplaceExisting);
                var outputString = JToken.FromObject(o).ToString();
                await FileIO.WriteTextAsync(sessionFile, outputString);
            }
        }
        catch (Exception e)
        {
            Debug.WriteLine("Encountered exception: {0}", e);
        }
    }

    public async Task<T> GetDataOrDefault<T>(string key, T defaultValue)
    {
        try
        {
            T results = defaultValue;

            var sessionFile = await _localFolder.CreateFileAsync(key, CreationCollisionOption.OpenIfExists);
            var data = await FileIO.ReadTextAsync(sessionFile);

            if (!String.IsNullOrWhiteSpace(data))
            {
                results = JToken.Parse(data).ToObject<T>();
            }

            return results;
        }
        catch (Exception e)
        {
            Debug.WriteLine("Encountered exception: {0}", e);
        }

        return defaultValue;
    }

dBlisse
  • 801
  • 5
  • 13