Question about running async methods in Maui

Lloyd Sheen 1,476 Reputation points
2024-03-26T15:30:43.27+00:00

I have an app which I can show information from a view on a ContentPage quickly. I have another thing I want to do but to get the data required takes about 5 seconds. What I want to do is on the load of the page start an Async method which while the user is looking at the easily accessable data is getting the harder to get data.

To get the data is not the problem and to start the method is also not a problem.

Now usually when I execute a method with Async I use await. But in this case I just want to start the method and let it do its thing. What I need to know is the following 2 things.

  1. When the user goes to the tab within the view that will display the hard to get data how will it know that the data is available.
  2. If the user descides to go to a different ContentPage within the app what happens to the method.
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,517 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 65,396 Reputation points
    2024-03-26T16:15:15.08+00:00

    you will need a global flag to track the status of the data fetch and store results in a global cache. use locks to make thread safe. your code should detect if a fetch is in progress and not start a new one. the fetch could also support events, to supply notification on competition.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.