1

I'm writing an Xamarin.Forms app and I'm having some trouble making a request using async. The freezes temporarily when the network request is made when it really shouldn't. What am I doing wrong?

public RecipesView LatestRecipes
    (string searchTerm, long? fromTimestamp, int recordsPerPage, bool hasMoreRecords)
    {
        HttpClientHandler handler = new HttpClientHandler();
        handler.CookieContainer = Settings.cookies;

        string url = Settings.Default.baseUrl + "/Api/recipes/latest";
        Dictionary<string, string> queryString = new Dictionary<string, string> ();
        queryString.Add ("maxRecords", recordsPerPage.ToString());
        queryString.Add ("searchTerm", searchTerm);
        queryString.Add ("username", "");
        queryString.Add ("boardSlug", "");
        queryString.Add ("type", "json");

        string queryUrl = url + ToQueryString(queryString);

        string result = DownloadString (queryUrl, handler).Result;

        RecipesView view = JsonConvert.DeserializeObject<RecipesView> (result);
        hasMoreRecords = view.HasMoreRecords;

        foreach (RecipeModel model in view.Records) {
            model.OriginalImageWidth = model.ImageWidth;
            model.OriginalImageHeight = model.ImageHeight;
        }

        return view;

    }

    public async Task<string> DownloadString(string url, HttpClientHandler handler)
    {
        var httpClient = new HttpClient(handler); // Xamarin supports HttpClient!

        Task<string> contentsTask = httpClient.GetStringAsync(url); // async method!

        // await! control returns to the caller and the task continues to run on another thread
        string contents = await contentsTask;

        return contents; // Task<TResult> returns an object of type TResult, in this case int
    }

Thanks, Colin.

1
  • 2
    Your consumption of DownloadString is not async. Calling .Result is waiting for the task to finish. Jan 23, 2015 at 16:50

1 Answer 1

1

With async/await, you need to go async all the way down. So you should change your LatestRecipes method to be async as well.

public async Task<RecipesView> LatestRecipesAsync(string searchTerm, long? fromTimestamp, int recordsPerPage, bool hasMoreRecords)
{
// ... Your existing code ...
    string result = await DownloadString (queryUrl, handler);
// ... The rest of your code ...
}

Also, the recommended convention for async methods is to postfix the name with Async.

2
  • Ah, I see, so how far do I need to go back? So for the parent method do I add an await like RecipesView rv = await recipeService.LatestRecipes (searchTerm, fromTimestamp, maxRecords, hasMoreRecords); and then add async to the method declaration too? Jan 23, 2015 at 17:10
  • Go as far back up as you can. In WinForms/WPF, you do async void on your event handlers and go async all the way down. I would guess Xamarin has something similar. Jan 23, 2015 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.