fandro fandro - 3 years ago 210
C# Question

Xamarin Android async await not await method

I m doing a Xamarin Android project, I wan't to display a list of objects from my SQLite database into a RecyclerView.

The RecyclerView is in a Fragment. I wan't to display the list when I open this fragment, and when I go back from an Activity to this fragment I wan't to refresh the data of the recycler view.

For know I get the correct values, but I don't know why the

GetMyObjectsAsync
result is not awaited Why ?

using System.Collections.Generic;
using System.Threading.Tasks;
using Android.OS;
using Android.Support.V7.Widget;
using Android.Views;
using Microsoft.Practices.ServiceLocation;
using System.Linq;
using Android.App;
using Android.Widget;
using Android.Util;
using System;

namespace POCProject.Droid.Fragments
{
public class MyCustomFragment : Fragment
{

private IMyObjectsService _myObjectsService;

List<ObjModel> myObjects { get; set; } = new List<ObjModel>();

RecyclerView mRecyclerView;

RecyclerView.LayoutManager mLayoutManager;

MyObjectsRecyclerViewAdapter mAdapter;



public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

_myObjectsService = ServiceLocator.Current.GetInstance<IMyObjectsService>();

}

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

View view = inflater.Inflate(Resource.Layout.fragment_my_objects_list, container, false) as View;

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


mLayoutManager = new LinearLayoutManager(mRecyclerView.Context);


mRecyclerView.SetLayoutManager(mLayoutManager);

return view;
}

public override void OnResume()
{
base.OnResume();
Task.Run(async () => {
try
{
myObjects = await GetMyObjectsAsync(); // Here it's not awaited the result why ?
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

});
mAdapter = new MyObjectsRecyclerViewAdapter(myObjects);
Console.WriteLine("--------------------OnResume Called-------------------- " + myObjects.Count()); // Always 0 why ?

mRecyclerView.SetAdapter(mAdapter);
}



private async Task<List<ObjModel>> GetMyObjectsAsync(){
Console.WriteLine("--------------------GetMyObjectsAsync Called--------------------");
var result = await _myObjectsService.GetAllAsync(); // Get objects in sqlite databases
Console.WriteLine("-------------------- result" + result.Value.Count() + "--------------------"); // Return the correct value for example 5

List<ObjModel> myOjectsTmp = new List<ObjModel>();


if (!result.HasErrors && result.HasValue)
{
foreach (var obj in result.Value)
{
myOjectsTmp.Add(obj);
}
Console.WriteLine("-------------------- myOjectsTmp"+ myOjectsTmp.Count() + "--------------------"); // Return the correct value for example 5
}

return myOjectsTmp;
}


}
}

Answer Source

Try this:

Move these two line:

mAdapter = new MyObjectsRecyclerViewAdapter(myObjects);
mRecyclerView.SetAdapter(mAdapter);

after

 myObjects = await GetMyObjectsAsync();

Like:

   myObjects = await GetMyObjectsAsync();
Activity.RunOnUiThread(() =>
                    {
    mAdapter = new MyObjectsRecyclerViewAdapter(myObjects);
    mRecyclerView.SetAdapter(mAdapter);
 });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download