I'm having multiple ObservableCollections in my app, which I would like to save, edit and so on. But since they're a collection of my own type of class, SQLite doesn't recognizes them. What would be the best way to save theses? Or what am I doing wrong.
Sort of looks something like this:
In my App: (Of course there's more code inside)
public partial class App : Application
{
static MyDatabase database;
public static MyDatabase Database
{
get
{
if (database == null)
{
database = new MyDatabase(DependencyService.Get
<IFileHelper>().GetLocalFilePath("MyListSQLite.db3"));
}
return database;
}
}
}
MyDatabase:
public class MyDatabase
{
readonly SQLiteAsyncConnection database;
public MyDatabase(string dbPath)
{
database = new SQLiteAsyncConnection(dbPath);
database.CreateTableAsync<MyClass>().Wait();
}
public Task<List<MyClass>> GetItemsAsync()
{
return database.Table<MyClass>().ToListAsync();
}
public Task<MyClass> GetItemAsync(int id)
{
return database.Table<MyClass>().Where(i => i.ID == id).FirstOrDefaultAsync();
}
public Task<int> SaveItemAsync(MyClass item)
{
if (item.ID != 0)
{
return database.UpdateAsync(item);
}
else
{
return database.InsertAsync(item);
}
}
public Task<int> DeleteItemAsync(MyClass item)
{
return database.DeleteAsync(item);
}
}
MyClass:
public class MyClass : ObservableCollection<MyOtherClass>
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
}
MyOtherClass:
public class MyOtherClass : BindableObject
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public static readonly BindableProperty ListNbrProperty =
BindableProperty.Create("ListNbr", typeof(int), typeof(MyOtherClass), default(int));
public int ListNbr
{
get { return (int)GetValue(ListNbrProperty); }
set { SetValue(ListNbrProperty, value); }
}
public bool IsMaster { get; set; }
public static readonly BindableProperty ResizedPathProperty =
BindableProperty.Create("ResizedPath", typeof(String), typeof(MyOtherClass), default(String));
public String ResizedPath
{
get { return (String)GetValue(ResizedPathProperty); }
set { SetValue(ResizedPathProperty, value); }
}
...
}