Problems with generic methods marked with async on iOS device

RickyKaareRickyKaare DKUniversity ✭✭
edited August 2013 in Prerelease & Betas

I don't know if this is a limitation of the AOT compiler in Xamarin.iOS, but running my project on a physical device revealed an issue whenever I call a method that has a generic parameter and is marked with the async keyword (allowing awaits).

Run-time I am getting this error:

Attempting to JIT compile method while running with --aot-only

An example of a method causing the problem:

public async Task<T> GetAsync<T>(string url) where T : ServiceResource
{
    using (var client = Session.Current.ServiceClient.Create())
    {
        return await client.GetAsync<T>(url);
    }
}

For now, I found a workaround that allows me to keep my original interface, by just moving the await to an internal method without the generic parameter, like this:

public Task<T> GetAsync<T>(string url) where T : ServiceResource
{
    return GetAsync(typeof(T), url).ContinueWith<T>(t => (T)t.Result);
}
private async Task<Object> GetAsync(Type outputType, string url) 
{
    using (var client = Session.Current.ServiceClient.Create())
    {
        return await client.GetAsync(outputType, url);
    }
}

OK. I just found a bug reported in bugzilla that covers this issue:
https://bugzilla.xamarin.com/show_bug.cgi?id=12746

I'll leave the post here for anybody that need the workaround, and hope for a fix soon ;o)

Posts

Sign In or Register to comment.