1

I wanted to fetch and display data but its not working and after this line

var response = await client.GetAsync(uri).ConfigureAwait(false);

code never executed not even throw an exception

public async Task<List<Restresponse>> RefreshDataAsync()
{
    var Items = new List<Restresponse>();
    try
    {
        HttpClient client = new HttpClient();
        var uri = new Uri(string.Format("https://jsonplaceholder.typicode.com/posts", string.Empty));
        var response = await client.GetAsync(uri).ConfigureAwait(false);
        if (response.IsSuccessStatusCode)
        {
            var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); ;
           // Items = JsonConvert.DeserializeObject<List<TodoItem>>(content);
        }
    }
    catch (Exception e)
    {
        Log.WriteLine(LogPriority.Debug, "EXception from restservice", e.Message);
    }
    return Items;

}

and i have called this functiion in OncreateView in fragment

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View view = inflater.Inflate(Resource.Layout.recycleview, container, false);

            mRecyclerView = view.FindViewById<RecyclerView>(Resource.Id.recyclerView);

            mLayoutManager = new LinearLayoutManager(this.Activity);
            mRecyclerView.SetLayoutManager(mLayoutManager);

            // Prepare the data source:
            listItem = new List<Restresponse>();
            try
            {
                listItem = RefreshDataAsync().Result;

                // Instantiate the adapter and pass in its data source:
                mAdapter = new RecycleViewAdapter(listItem, this.Activity);

                // Plug the adapter into the RecyclerView:
                mRecyclerView.SetAdapter(mAdapter);
            }
            catch (System.Exception e)
            {
                Log.WriteLine(LogPriority.Debug, "EXception from fragmenbt", e.Message);
            }
            return view;
        }
5
  • 1
    Did you add internet concection autorization ? Commented Sep 19, 2017 at 12:22
  • Also how and where is RefreshDataAsync() being called? If you are mixing blocking and async calls then most likely you are experiencing a deadlock. Commented Sep 19, 2017 at 13:09
  • Have you tried taking of the await and calling the code synchronously and stepping through it to make sure it works as expected? So var response = client.GetAsync(uri).Result? Commented Sep 19, 2017 at 13:45
  • i have already added permission Also RefreshDataAsync() is called in main thread i think Commented Sep 20, 2017 at 5:22
  • @NitinGupta RefreshDataAsync().Result is causing a deadlock do not mix blocking calls like .Result with async-await code as they tend to lead to deadlocks Commented Sep 20, 2017 at 9:49

1 Answer 1

2

Sure you are getting a deadlock.

If you are calling from constructor or event RefreshDataAsync().Result; you should remove .ConfigureAwait(false); from function because if you are in main thread it will never finish.

another option after comments I think that if you need async process your best way is use OnResume after creating view.

    public async override void OnResume()
    {
        base.OnResume();
        try
        {
            listItem = RefreshDataAsync().Result;

            // Instantiate the adapter and pass in its data source:
            mAdapter = new RecycleViewAdapter(listItem, this.Activity);

            // Plug the adapter into the RecyclerView:
            mRecyclerView.SetAdapter(mAdapter);
        }
        catch (System.Exception e)
        {
            Log.WriteLine(LogPriority.Debug, "EXception from fragmenbt", e.Message);
        }
    } 

Don't forget remove Try/catch block from OnCreateView where you call RefreshDataAsync()


Full code fragment:

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {

            try
            {
                View view = inflater.Inflate(Resource.Layout.recycleview, container, false);

                mRecyclerView = view.FindViewById<RecyclerView>(Resource.Id.recyclerView);

                mLayoutManager = new LinearLayoutManager(this.Activity);
                mRecyclerView.SetLayoutManager(mLayoutManager);
            }
            catch (Exception)
            {

                Log.WriteLine(LogPriority.Debug, "EXception from fragment inside OnCreateView", e.Message);
            }



        }

        public async override void OnResume()
        {
            base.OnResume();
            // Prepare the data source:
            try
            {
                using (var client = new HttpClient())
                {
                    var uri = "jsonplaceholder.typicode.com/posts";
                    var result = await client.GetStringAsync(uri);
                    var Restresponse = JsonConvert.DeserializeObject<List<Restresponse>>(result);
                    mAdapter = new RecycleViewAdapter(Restresponse, this.Activity);
                    mRecyclerView.SetAdapter(mAdapter);
                }
            }
            catch (System.Exception e)
            {
                Log.WriteLine(LogPriority.Debug, "EXception from fragment inside OnResume", e.Message);
            }
        }
Sign up to request clarification or add additional context in comments.

18 Comments

I have simply called RefreshDataAsync() and after executing below line control never return var response = await client.GetAsync(uri).ConfigureAwait(false);
and if i make a simple call its through an exception
Are you in main thread, UI thread. have you tried to remove ConfigureAwait(false) await client.GetAsync(uri).ConfigureAwait(false) is recommended when you get deadlock. but if you are getting it with this sentence a deadlock may be you should try normal way without configureawait
I have removed and still the same control never return after var response = await client.GetAsync(uri);
You are calling inside an event or in page constructor?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.