locked
Local Storage - WIn 8 C# RRS feed

  • Question

  • I have a published app in Windows Phone store and I'm porting the app to windows 8. There are set of values that needs to be saved persistently.. The smaller values(few counts, few settings) I'm saving in the ApplicationDataContainer. I have a datetime array that I need to save. But every time the user enters a date I'm saving that date into an array. The array is then written to a text file.

    Since I cant find a single exit of the app like the OnBackKeyPress in WP, I'm writing the the array to the file as soon as the user is entering the date. So when the user continuously enters dates, it opens file, write array each time.. When the user opens the app, it reads it from the file and updates the UI according to the dates.

    Now the problem is.. Suppose you continously enter dates, then close the app quickly and when you open it next time, only few dates are saved.. rest of the dates have junk values. But the file writing is properly done when im debugging using break points.. It also works when I give a time gap of about 1 or 2 seconds before I enter the next date.

    Is it because that when not debugging, the UI load is faster? And since the background file writing is not yet finished it jus gets terminated.?? How do I solve this? I do I save the array of dates?

    This is how I'm trying to i write the dates into file. WriteHistoryToFile is called everytime I set a date. 

            public async Task<bool> WriteHistoryToFile()
            {
                bool fileWrite = false;
                if (m_iHistCount > 0)
                {
                    try
                    {
                        StorageFile sampleFile = await localfolder.CreateFileAsync("HistoryFile", CreationCollisionOption.ReplaceExisting);
    
                        var stream = await sampleFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
    
                        using (var outputStream = stream.GetOutputStreamAt(0))
                        {
    
                            DataWriter dataWriter = new DataWriter(outputStream);
    
                            for (int i = 0; i < m_iHistCount; i++)
                                dataWriter.WriteString(m_dtHistory[i].ToString("d", CultureInfo.InvariantCulture) + "`~");
    
                            await dataWriter.StoreAsync();
                            await dataWriter.FlushAsync();
                            dataWriter.Dispose();
                            await outputStream.FlushAsync();
                            fileWrite = true;
                        }
    
                    }
                    catch (Exception e)
                    {
                        string str = e.Message;
                        fileWrite = false;
                    }
                    return fileWrite;
                }
                else
                {
                    return fileWrite;
                }
    
            }
    Ideally when should I save the date into file? or can I save this array to the ApplicationData container. The array is of size 400.

    Alfahisham

    Wednesday, November 6, 2013 7:49 AM

Answers

  • Hi Alfahisham,

    isn´t the OnSuspending method in App.xaml.cs not working for you?

    private async void OnSuspending(object sender, SuspendingEventArgs e)
            {
                var deferral = e.SuspendingOperation.GetDeferral();
                try
                {
                    await WriteHistoryToFile();
                    await SuspensionManager.SaveAsync();
                }
                finally
                {
                    deferral.Complete();
                }
            }

    Or does it take to long to save the history?

    lh

    • Marked as answer by Alfahisham Tuesday, November 12, 2013 6:05 AM
    Wednesday, November 6, 2013 7:44 PM

All replies

  • Hi Alfahisham,

    isn´t the OnSuspending method in App.xaml.cs not working for you?

    private async void OnSuspending(object sender, SuspendingEventArgs e)
            {
                var deferral = e.SuspendingOperation.GetDeferral();
                try
                {
                    await WriteHistoryToFile();
                    await SuspensionManager.SaveAsync();
                }
                finally
                {
                    deferral.Complete();
                }
            }

    Or does it take to long to save the history?

    lh

    • Marked as answer by Alfahisham Tuesday, November 12, 2013 6:05 AM
    Wednesday, November 6, 2013 7:44 PM
  • Thank you Lars! it works.. :)

    Alfahisham

    Friday, November 15, 2013 6:32 AM