Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 81910

How to use async await for getting data for a content page

$
0
0

Dear,
I am learning using Sqlite-net for PCL and listview as the TODO List example.
I was based on that and create my own project. and modify the item cell with a card view as show in this link
https://www.syntaxismyui.com/xamarin-forms-in-anger-cards/

The think is i have to use for loop to add the children to stacklayout and also have to get the data from the database also
the list now is just 30 items but it seems too low to load it all.
In Android it said "Too much work on the main thread" but in IOS it run quite smooth.
Can anyone help me to use with async await as similar as the AsyncTask in Android. I tried to use async await but it say that it run in synchronous.
Here is the code:

``public class HistoryPage : ContentPage
{

    List<Machine> list = new List<Machine>();
    StackLayout layout = new StackLayout ();
    public HistoryPage ()
    {
        Padding = new Thickness (10, 10, 10, 10);
        var viewmodel = new HistoryViewModel ();
        Title = "History";


        var ignore = LoadData ();
        layout.VerticalOptions = LayoutOptions.FillAndExpand;
        Content = new ScrollView{
            Content = layout,
        };

    }

    protected override void OnAppearing ()
    {
        base.OnAppearing ();
        ((App)App.Current).ResumeAtMachineId = -1;

        list = (List<Machine>) App.Database.GetItemsInDesc (20);
        foreach(var machine in list){
            layout.Children.Add (new CardView(machine));
        } 
    }
    public async Task<List<Machine>> GetDataFromDatabase() // (" Async block lacks await operator and will run synchronously")<del></del>
    {

            return  (List<Machine>) (App.Database.GetItemsInDesc(20));
    }
    async Task LoadData(){
        Task<List<Machine>> abc = GetDataFromDatabase ();
        list = await abc;
        foreach(var machine in list){
            layout.Children.Add (new CardView(machine));
        }

    }
}```

Viewing all articles
Browse latest Browse all 81910

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>