Issue with Async

PaulPPaulP USMember ✭✭
edited June 2014 in Xamarin.Forms

I am experiencing a very strange issue using async to make a call out to Google Places API.

Summary :
Async call never comes back. No Exception thrown

Page :

List<Place> GetCarWashes ()
{
    var googlePlacesService = new GooglePlacesService (new System.Net.Http.HttpClient ());

    var carWashesTask = googlePlacesService.GetCarWashesAsync (location);
    carWashesTask.Wait ();
    return carWashesTask.Result;
}

The call to GetCarWashesAsync never comes back.

Service Implementation :

public async Task<List<Place>> GetCarWashesAsync (Position location)
{
    var uri = string.Format (GooglePlacesUrl, location.Latitude,
              location.Longitude, API_KEY);
    var result = await _httpClient.GetAsync<PlacesRootObject> (uri);

    return result.Places;
}

HttpClient :

    public static async Task<T> GetAsync<T> (this HttpClient client, string url)
    {
        var httpRequest = new HttpRequestMessage (new HttpMethod ("GET"), url);

        client.Timeout = TimeSpan.FromSeconds (30);

        var response = await client.SendAsync (httpRequest);

        var jsonString = response.Content.ReadAsStringAsync ();

        jsonString.Wait ();

        Debug.WriteLine (jsonString.Result);

        var result = JsonConvert.DeserializeObject<T> (jsonString.Result);

        return result;
    }

Full Example with Unit Test :

Service :

https://github.com/paulpatarinski/ShouldIWashMyCar/blob/master/Core/Services/GooglePlacesService.cs

Service Test :

https://github.com/paulpatarinski/ShouldIWashMyCar/blob/master/Core.Test/GooglePlacesServiceTest.cs

HttpClientExtensions :

https://github.com/paulpatarinski/ShouldIWashMyCar/blob/master/Core/Helpers/HttpClientExtensions.cs

Page :

https://github.com/paulpatarinski/ShouldIWashMyCar/blob/master/Core/Pages/CarWashesMapPage.cs

The weird part is I am able to make the exact same call from a unit test just fine, but when debugging using device : the call never comes back.

Cheers,
Paul

Posts

  • CraigDunnCraigDunn USXamarin Team Xamurai

    What platform is the device (Android/iOS)? Does it also fail in the simulator? If Android, have you set the correct permissions (probably just INTERNET in this case)?

    Also, just curious, why do you call

    jsonString.Wait ();
    

    instead of

    var result = JsonConvert.DeserializeObject<T> (await jsonString);
    

    ?

  • PaulPPaulP USMember ✭✭
    edited June 2014

    Devices :

    Nexus 5 running 4.4.4 
    
    Samsung S4 running 4.2.2 
    

    Also fails in the simulator.

    And just to be clear it is not a hard failure (don't get any exception ) it just reaches the call in the HttpClientExtensions

    var response = await client.SendAsync (httpRequest);
    

    Makes that call and never comes back.

    As far as await vs Wait(): Had some challenges with unit testing when using await, so trying to stay away from it :)

  • PaulPPaulP USMember ✭✭

    @CraigDunn here as example of some async weirdness :

    http://forums.xamarin.com/discussion/19545/nunit-weirdness
    
Sign In or Register to comment.