You can get, set or even subscribe a KeyValue state without writing actions nor reducers.
// a helper which can collect all your subscriptions
var subscriptions = new SubscriptionManager();
using (var db = new RealtimeLiteDatabase(new MemoryStream()))
{
// subscribe to username
subscriptions += db.StateStore<string>("username").Subscribe(username =>
Dispatcher.BeginInvokeOnMainThread(() =>
{
userLabel.Text = username;
}));
db.StateStore<string>("username").Set("FuturistiCoder");
// userLabel.Text will be 'FuturistiCoder';
}
StateStore<T>
: the T
can be reference type
or value type
. But if you want to save and listen collections, use db.Realtime.Collection<T>
instead (LiteDB.Realtime).
subscriptions += db.Realtime.Collection<TodoItem>("TodoItems").Subscribe(items =>
Dispatcher.BeginInvokeOnMainThread(() =>
{
listView.ItemsSource = items;
})
// listView will be updated after TodoItems changed
);
Call subscriptions.Dispose()
will dispose all your added subscriptions.