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

Task exists before get the Model from the Sqlite database.

$
0
0

DataContext to Sqlite database
public class Datastore<T> where T: Model, new() { // Other codes public async Task<T> GetAsync(int id) { return await _database.Table<T>().Where(i => i.Id == id).FirstOrDefaultAsync(); } }

The category view model where the problem occurs
```public class CategoryPageViewModel : BaseViewModel
{
public CategoryPageViewModel() {}

    public CategoryPageViewModel(int id)
    {
        Category category = new Datastore<Category>().GetAsync(id).Result; // Here happens the Task exists
        //Title = category.Name;
    }
}```

The above view model called by this method:
[XamlCompilation(XamlCompilationOptions.Compile)] public partial class CategoryMenuView : ContentView { // other codes private async void SetCategoryMenuElements() { IList<Category> categories = await new Datastore<Category>().GetAllAsync(); // This works CategoryMenu.Children.Add(new Button { Text = categories.ElementAt(0).Name, Command = new Command(() => LoadCategoryPage(categories.ElementAt(0).Id)), Style = plainButtonTablet }, 0, 0); } private async void LoadCategoryPage(int id) { await Navigation.PushAsync(new CategoryPage(id)); } }

I think the problem with my Task, Thread knowledge. But I do not know how to solve this problem.
If I do not use the Sqlite GetAsync calling and using a static Category model, then the app will works. With GetAsync method it is just stop without any error, I mean I always see the categories page.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



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