0

I am trying to parse data from a json file but I am getting variable outputs(Sometimes right, othertimes nothing). I am pretty much sure it is related with the time needed to parse the file, but having trouble finding out where. Here it is-

public class HspitalVM
{
    List<Hspital> hspitalList=null;
    public List<KeyedList<string, Hspital>> GroupedHospitals
    {
        get
        {
            getJson();
            var groupedHospital =
                from hspital in hspitalList
                group hspital by hspital.Type into hspitalByType
                select new KeyedList<string, Hspital>(hspitalByType);

            return new List<KeyedList<string, Hspital>>(groupedHospital);
        }
    }


    public async void getJson()
    {
        StorageFolder localFolder = ApplicationData.Current.LocalFolder;

        try
        {
            StorageFile textFile = await localFolder.GetFileAsync(m_HospFileName);

            using (IRandomAccessStream textStream = await textFile.OpenReadAsync())
            {
                using (DataReader textReader = new DataReader(textStream))
                {

                    uint textLength = (uint)textStream.Size;
                    await textReader.LoadAsync(textLength);
                    string jsonContents = textReader.ReadString(textLength);

                    hspitalList = JsonConvert.DeserializeObject<IList<Hspital>>(jsonContents) as List<Hspital>;


                }
            }
        }
        catch (Exception ex)
        {
            string err = "Exception: " + ex.Message;
            MessageBox.Show(err);
        }

    }
 }
Lamia Mehreen
  • 759
  • 1
  • 8
  • 15
  • Your `async` function should return a `Task`. Change your get call to `await getJson();` – mrtig Dec 23 '13 at 00:28
  • You may find my [`async` properties](http://blog.stephencleary.com/2013/01/async-oop-3-properties.html) blog post helpful (in addition to my [`async` intro](http://blog.stephencleary.com/2012/02/async-and-await.html) post). – Stephen Cleary Dec 24 '13 at 01:15

1 Answers1

1

You are not await-ing result of getJson() call, so as expected most of the times you'll get no information because actual call to GetFileAsync is not completed yet.

Now since getJson method returns void you can't really await on it. Potential fix by using Result to turn asynchronous code into synchronous for get:

public List<KeyedList<string, Hspital>> GroupedHospitals
{
    get
    {
        hspitalList= getJson().Result;
        ...
    }
}
...
public async Task<IList<Hspital>> getJson()
{
   ....
   return JsonConvert.DeserializeObject<IList<Hspital>>(jsonContents) as List<Hspital>;
}

Note: It is generally bad idea to make get methods to perform long operations, also calling async method in synchronous way via Wait/Result can cause deadlocks in your code -await vs Task.Wait - Deadlock?.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • its already causing deadlocks,i.e. its freezing the UI. I know the dos and donts of asynchronous coding but having trouble implementing them. – Lamia Mehreen Dec 23 '13 at 01:04
  • @LamiaMehreen - note that "freezing UI" and [deadlock](http://en.wikipedia.org/wiki/Deadlock) are two different concepts - if "freezing" eventually will stop (when long operation is complete), while "deadlock" will never resolve itself. – Alexei Levenkov Dec 23 '13 at 01:55