Reputation: 287
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;
}
Upvotes: 1
Views: 320
Reputation: 496
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);
}
}
Upvotes: 2