1

First of all, I did a little research but only found for Swift a native iOS answer.

The complete warning says:

This method can cause UI unresponsiveness if invoked on the main thread. Instead, consider waiting for the `-locationManagerDidChangeAuthorization:` callback and checking `authorizationStatus` first.

I have read that could be because I am using an async, away call; as well. I have read about the iOS version also: StackOverFlow/73805219 How I can fix this in Xamarin forms?

I must say that I am using Geolocalization from #Xamarin.Forms.Essentials to get the current longitud and latitude in an async function that I send by #MessaginCenter.

async Task StoringNoteAsync()
        {
            Location location = await _geolocation.GetCurrentLocation();


            NoteSelected = NoteSelected ?? new Note();


            NoteSelected.Title      = Title;
            NoteSelected.Content    = Content;
            NoteSelected.CreatedAt  = DateTime.Now;
            NoteSelected.iNoteType  = (int)SelectedNoteType;
            NoteSelected.Longitude  = location.Longitude;
            NoteSelected.Latitude   = location.Latitude;


            //_noteService.SaveNote( NoteSelected );


            MessagingCenter.Instance.Send( this, "upsert", NoteSelected );


            await _navigation.PopAsync();
        }
  • I have no idea what problem you're trying to fix. You make a vague reference to a "warning" in the title, but you haven't posted any explicit warning or error message in the body of your post. – Jason Jun 08 '23 at 14:47
  • TY Jason, to take your time to answer this question; I appreciate it, you are right. I updated the question :v, Sorry, it is my second question in this platform :( – Carlos M Gomez Jun 08 '23 at 14:50
  • What's the version of your `Xamarin.Essentials` nuget? You can try to update it to the latest version. – Jessie Zhang -MSFT Jun 09 '23 at 02:40

1 Answers1

0

Sounds like you are on the UI thread, and need to run code on a different thread.

Use Task.Run to get to a background thread.

Since you are in an async method, and you are already running on MainThread, do it like this:

async Task MyMethod()
{
    // "await", so that "additional code" below is not run until this code finishes.
    // "async" is only needed if code in block uses "await".
    await Task.Run( async () =>
    {
        // code that needs to run on a background thread...
    });

    // additional code, that needs to run on the original thread...
    ...
}

ALTERNATIVE TO EXPLICITLY RUN ADDITIONAL CODE ON MAIN THREAD

async Task MyMethod()
{
    // (This part is same as first code snippet.)
    // "await", so that "additional code" below is not run until this code finishes.
    // "async" is only needed if code in block uses "await".
    await Task.Run( async () =>
    {
        // code that needs to run on a background thread...
        // e.g. long-running logic that does not touch UI.
    });

    // (This part is explicitly run on MainThread.)
    // "async" is only needed if code in block uses "await".
    await MainThread.InvokeOnMainThreadAsync( async () =>
    {
        // code that needs to run on MainThread (affects UI)...
    });
}
ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196