Jeeva123
Jeeva123

Reputation: 1047

How to Store Object to windows phone 8.1

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

Upvotes: 0

Views: 323

Answers (1)

dBlisse
dBlisse

Reputation: 811

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;
    }

Upvotes: 1

Related Questions