2

I have a problem, where i call an async method, and the call does not return back. I assume its a race of threads. How do I write this correctly?

This is where it starts. I first call an async method called "GetCachedValuesAsync"

public void OnNavigatingTo(NavigationParameters parameters)
    {
        Task.Run(async () =>
        {
            await GetCachedValuesAsync();
            ClipRefernce = GenerateRefernce(clips);
        });
    }

Here is the method signature for GetCachedValueAsync

public async Task  GetCachedValuesAsync()
    {
        try
        {
            clips = await BlobCache.LocalMachine.GetObject<List<Clip>>("clips");
        }
        catch (KeyNotFoundException ex)
        {
            clips = new List<Clip>();
        }
    }

I do not get the call returned from BlobCache, BlobCahce method is part of a library called akavache.

The code also does not hit: ClipRefernce = GenerateRefernce(clips);

I appreciate your help

Edit 1

This is GenerateRefernce method.

public string  GenerateRefernce(List<Clip> clips)
    {
        string refernce = "";
        if(clips.Count > 0)
        {
            var clip = clips.LastOrDefault();
            refernce = String.Format("Ref {0:yyyy}/{1}",DateTime.Now , clip.ClipId + 1);
        }
        else{
            refernce = String.Format("Ref {0:yyyy}/{1}", DateTime.Now, 1);
        }
        return refernce;
    }
Libin Joseph
  • 7,070
  • 5
  • 29
  • 52
  • 2
    Are you getting any exception? – Amit Kumar Singh Oct 06 '17 at 05:14
  • @Amit : I dont get any exception – Libin Joseph Oct 06 '17 at 05:17
  • @Amit : I have made those changes. But that does not resolve the issues – Libin Joseph Oct 06 '17 at 05:33
  • This might help. https://stackoverflow.com/questions/31425210/akavaches-getobjectt-hangs-when-awaited-any-idea-what-is-wrong-here – Amit Kumar Singh Oct 06 '17 at 06:00
  • Though the questions sounds exactly the same issue, i have tried both the solutions in the page, but does not work – Libin Joseph Oct 06 '17 at 06:53
  • I can't see any reason why the answer you've accepted would make any difference. – Jon Skeet Oct 06 '17 at 10:28
  • I've seen an issue like this before with the first call into Akavache because of how the BlobCache is initialized causing a deadlock. https://github.com/akavache/Akavache/issues/216. Can you try installing the latest version 5 bits from myget and see if you have the same issue? Or install the alpha version 6 from nuget ? Or try initializing Akavache early in your app startup and just make a dummy call to LocalCache like LocalCache.Insert("key", new object()).subscribe(). If none of that works try pausing the application and looking at the threads to see where it's deadlocked – Shane Neuville Oct 06 '17 at 15:25
  • I really did not get to resolve this issue. I ended up switching to sqlite with ef core2. Works great for my need now – Libin Joseph Oct 10 '17 at 00:13

1 Answers1

0

You need to remove the sync method from the Task.Run like this:

public void OnNavigatingTo(NavigationParameters parameters)
{
    Task.Run(async () =>
    {
        await GetCachedValuesAsync();
    });

    ClipRefernce = GenerateRefernce(clips);
}

public async Task  GetCachedValuesAsync()
{
    try
    {
        clips = await BlobCache.LocalMachine.GetObject<List<Clip>>("clips");
    }
    catch (KeyNotFoundException ex)
    {
        clips = new List<Clip>();
    }
}
Giusepe
  • 655
  • 5
  • 15