Hi guys,
I've got an issue where I've been following the guide on working with a local database, as well as the associated repo.
I've managed to connect to the database and add items to it, but it would seem as if the list that I'm returning from the database always has a count of 0.
As mentioned the database adds items just fine. In the following snippet I'm adding the result of a HTTP Get-request in the form of a deserialized JSONarray (currently a List):
protected override async void OnAppearing()
{
base.OnAppearing();
App.Database.Tester();
var result = await App.Database.GetItemsAsync();
Debug.WriteLine("Debugger: " + result.Count);
ListView lv = lvResults;
// Reset the 'resume' id, since we just want to re-start here
lvResults.ItemsSource = result;
}
Sorry for the ugly code. As mentioned above the count is always 0. Even if I've run through saving all the items:
var result = await GetAllFoundationsAsync();
for (var i = 0; i < result.Count; i++)
{
Debug.WriteLine(result[i].ToString()); //overridden, so shows a few values of each foundation in a pretty-string.
await App.Database.SaveItemAsync(result[i]);
}
after which I can see that the output shows me each individual result and add it to the database with no visible error.
After having added all the items I then see if the code in onAppearing() will give me anything; but it still does nothing. So I've stepped through the code both from a default perspective (without saving anything before-hand) and after having saved them to the database.
Below you will see my database and object-classes. Before that I want to mention that I've stepped through the whole project and have not encountered any issues; the breakpoints get invoked in both onAppearing and in the database-class (both in the SaveItemsAsync and in GetItemsAsync), but either the database is truly empty, or I'm doing something wrong somewhere else...
This is the foundation class:
namespace App3
{
public class Foundation
{
[PrimaryKey, AutoIncrement]
public int? ID { get; set; }
public int? FoundationID { get; set; }
//There are more attributes, but it's not necessary to show them all.
public string Summary
{
get {
Debug.WriteLine("Getting Summary");
return String.Format("Id: {0}, Added by {1}", ID, EngineerName);}
}
public override string ToString()
{
var out_string = string.Format("ID: {0},@Engineer Name: {1},@Signature: {2},@Date: {3}", ID, EngineerName, EngineerSignature, EngineerDate);
out_string.Replace("@", Environment.NewLine);
return out_string;
}
}
public class FoundationObject
{
public Foundation[] foundations { get; set; }
}
}
And this is the database-class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using SQLite;
namespace App3
{
public class FoundationsDatabase
{
readonly SQLiteAsyncConnection db;
public FoundationsDatabase(string dbPath)
{
db = new SQLiteAsyncConnection(dbPath);
db.CreateTableAsync<Foundation>().Wait();
}
public Task<List<Foundation>> GetItemsAsync()
{
var data = db.Table<Foundation>().ToListAsync();
Debug.WriteLine("DATA: " + data);
return db.Table<Foundation>().ToListAsync();
}
public void Tester()
{
Debug.WriteLine("QUERY: " + db.QueryAsync<Foundation>("SELECT * FROM [Foundation]"));
}
public Task<List<Foundation>> GetItemsAsync(int id)
{
return db.Table<Foundation>().Where(i => i.ID == id).ToListAsync();
}
public Task<int> SaveItemAsync(Foundation item)
{
if (item.ID != 0){
return db.UpdateAsync(item);
} else{
return db.InsertAsync(item);
}
}
public Task<int> DeleteItemAsync(Foundation item){
return db.DeleteAsync(item);
}
}
}