0

I have a static class with a async method in it . The async method awaits a user action (user needs to confirms on a dialog). I need to call this method in a constructor in App.xaml.cs (xamarin)

public static class StoragePermission
{ 
    public static async Task Check()
    {
       try
        {
            var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);
            if (status != PermissionStatus.Granted)
            {

                var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Storage });
                status = results[Permission.Storage];
            }
        }
        catch (Exception ex) {}
    }
}

And I have tried calling this method in the constructor like this:

StoragePermission.Check().Wait();

But doing the above , I dont get the next code to invoke at all. After the user action, focus does not return to the code.

I have also tried it this way:

public Task StorageInitialization { get; private set; }

and then within constructor, I do

StorageInitialization = StoragePermission.Check();

But doing this code returns to the next line in constructor, but does not wait for the user action to be completed.

I appreciate if someone could help me with this

Libin Joseph
  • 5,670
  • 4
  • 20
  • 44
  • `StoragePermission.Check().Wait();` is sync method call. Are you sure next code is not invoked? – Backs Mar 26 '18 at 23:39
  • 3
    @Backs calling `.Wait` on Task returned from asynchronous method normally deadlocks in UI apps... I'm not sure if it is the case in Xamarin, but definitely WPF/WinForms/ASP.Net will deadlock as OP described. – Alexei Levenkov Mar 26 '18 at 23:46
  • 1
    @AlexeiLevenkov Xamarin follows the same cases as WPF, WinForms, etc, so yes – Camilo Terevinto Mar 26 '18 at 23:48
  • Libin Joseph, have you seen https://stackoverflow.com/questions/23048285/call-asynchronous-method-in-constructor (which could be simply duplicate...) – Alexei Levenkov Mar 26 '18 at 23:48
  • @Backs : I am pretty sure the code does not hit the next line in the constructor. – Libin Joseph Mar 26 '18 at 23:55
  • This seems pretty much a duplicate of my question: https://stackoverflow.com/questions/38088542/httpclient-getasync-never-returns-on-xamarin-android – Camilo Terevinto Mar 26 '18 at 23:56
  • @AlexeiLevenkov : i did see that, and I had a look into his blog too, but did not quite get how to resolve the issue. Its from his blog i tried the property initialization. I would appreciate if you throw in some more light – Libin Joseph Mar 26 '18 at 23:58
  • @LibinJoseph Awaiting a UI task before the `Application` initialization is finished a really bad idea. Allow the App to continue to your first page/screen and perform the your permission check there. – SushiHangover Mar 27 '18 at 01:16
  • @SushiHangover : I realise that, There has been a fair bit code down with this before I have joined, and there is a need to access the DB before the first page loads. so it important I call the storage permissions before that – Libin Joseph Mar 27 '18 at 01:25
  • 2
    I'm pretty sure that trying to call async code from a constructor is going to cause bad things to happen. – Bradley Uffner Mar 27 '18 at 01:28
  • Here are a few useful looking patterns for `async` "constructors". http://blog.stephencleary.com/2013/01/async-oop-2-constructors.html – Bradley Uffner Mar 27 '18 at 01:31
  • @LibinJoseph The DB is outside of the app's sandbox? but, either way, what happens when the user denies permission? The Form's Application (and thus the platform's UIApplicationDelegate or Application) need to init. Test if you have permission first, if yes proceed to the "normal" first page, otherwise hit a permission landing page and prompt the user there and proceed the the normal first page. – SushiHangover Mar 27 '18 at 01:31

0 Answers0