Skip to content

Commit 3800af1

Browse files
committed
feat: Showcase asynchronous file system initialization in LiteDB sample
1 parent cae04df commit 3800af1

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

UI/LiteDB/LiteDB/src/LiteDBSample.Shared/MainPage.xaml.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.UI.Xaml;
66
using Microsoft.UI.Xaml.Controls;
77
using LiteDB;
8+
using Microsoft.UI.Xaml.Navigation;
89

910
namespace LiteDBSample
1011
{
@@ -21,7 +22,20 @@ public MainPage()
2122
_todoItems = new ObservableCollection<TodoItem>();
2223
}
2324

24-
private LiteDatabase _liteDatabase;
25+
protected override async void OnNavigatedTo(NavigationEventArgs e)
26+
{
27+
base.OnNavigatedTo(e);
28+
29+
// Before the database is accessed for the first time on WASM, the filesystem needs to be
30+
// initialized, otherwise data might be lost. Ensure that any asynchronous operation is
31+
// awaited before creating/accessing the database, as await will wait for the initialization to complete.
32+
// See https://platform.uno/docs/articles/features/file-management.html#webassembly-file-system for more information.
33+
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
34+
var data = await localFolder.CreateFolderAsync("Data", CreationCollisionOption.OpenIfExists);
35+
LoadFromDatabase(this, null);
36+
}
37+
38+
private LiteDatabase _liteDatabase;
2539

2640
private LiteDatabase LiteDatabase
2741
{
@@ -36,7 +50,7 @@ private LiteDatabase LiteDatabase
3650
}
3751
}
3852

39-
private static string DbPath => System.IO.Path.Combine(ApplicationData.Current.LocalFolder.Path, "save.db");
53+
private static string DbPath => System.IO.Path.Combine(ApplicationData.Current.LocalFolder.Path, "Data", "save.db");
4054

4155
private void LoadFromDatabase(object sender, RoutedEventArgs args)
4256
{

0 commit comments

Comments
 (0)