I am trying to implement a database following this: https://docs.microsoft.com/nl-nl/xamarin/xamarin-forms/app-fundamentals/databases
But I am getting an aggregate.exception on the main() function when debugging on my iphone.
I added this to my app.cs:
`
static ItemDatabase database;
public static ItemDatabase Database
{
get
{
if (database == null)
{
database = new ItemDatabase(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WaarStaatSQLite.db3"));
}
return database;
}
}`
Then this is the database class:
` public class ItemDatabase
{
readonly SQLiteAsyncConnection database;
public ItemDatabase(string dbPath)
{
database = new SQLiteAsyncConnection(dbPath);
database.CreateTableAsync<Item>().Wait(); // aggregate.exception probably from this line
Debug.WriteLine("Created Database");
}
//get all items from db async
public async Task<ObservableCollection<Item>> GetItemsAsync()
{
Debug.WriteLine("get items");
var data = await database.Table<Item>().ToListAsync();
Debug.WriteLine(data.ToString());
if(data != null)
{
return new ObservableCollection<Item>(data);
}
else
{
return null;
}
}
//get all items from db not async
public async Task<ObservableCollection<Item>> GetItemsNotDoneAsync()
{
var data = await database.QueryAsync<Item>("SELECT * FROM [Item] WHERE [Done] = 0");
return new ObservableCollection<Item>(data);
}
//get item from db with id
public async Task<Item> GetItemAsync(int id)
{
return await database.Table<Item>().Where(i => i.ID == id).FirstOrDefaultAsync();
}
//save or update a single item
public async Task<int> SaveItemAsync(Item item)
{
if (item.ID != 0)
{
return await database.UpdateAsync(item);
}
else
{
return await database.InsertAsync(item);
}
}
//delete item in db
public async Task<int> DeleteItemAsync(Item item)
{
return await database.DeleteAsync(item);
}
}`
I then call this function in the MainPageViewModel:
public override async void OnNavigatedTo(INavigationParameters parameters) { var data = await App.Database.GetItemsAsync(); if (data != null) { Debug.WriteLine(data.ToString()); Items = data; } }
OnNavigatedTo is a prism function.
I first thought maybe the OnNavigatedTo function can't be async but I found a question only saying the OnNavigatedTo function could be async void.
So I did some debugging using breakpoints and print statements and I found that the code stops in the database constructor on the createTable line. Because the Debug.writeline is never executed. Using a breakpoint on that line didn't help because i couldn't step in on the function.
I don't know if I needs to implement something in the ios-project because the sample didn't include anything.
Any help implementing databases is appreciated.