Skip to content

Commit 715c9e0

Browse files
committed
docs: Explain asynchronous file system initialization on WebAssembly
1 parent 3800af1 commit 715c9e0

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

UI/LiteDB/LiteDB/LiteDB.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,20 @@ var itemsWhichShouldBeDoneByToday =
145145
where !item.IsDone && item.DeadLine <= DateTime.Now
146146
select item).ToList();
147147
```
148-
Of course you can also use the method chaining instead of the query language. The core idea is, that `Query()` returns you a `IQueryable`, which let's you use all the power of LINQ and friends. And there you have it, the easy and rapid power of **LiteDB** paired with the easiness and power of the **UNO Platform**.
148+
Of course you can also use the method chaining instead of the query language. The core idea is, that `Query()` returns you a `IQueryable`, which let's you use all the power of LINQ and friends. And there you have it, the easy and rapid power of **LiteDB** paired with the easiness and power of the **Uno Platform**.
149+
150+
## File System Initialization on WebAssembly
151+
152+
On WebAssembly, the file system is initialized asynchronously while the application is launching. Attempts to access the file system before this operation finalizes (e.g. during `Application.OnLaunched` or while the main page of the application is navigated to) may have unexpected results and could even cause a crash.
153+
154+
To prevent this, make sure to perform and await an asynchronous operation on `ApplicationData.Current.LocalFolder` first. A good example would be to store the database in a nested `Data` folder, which needs to be asynchronously created first:
155+
156+
```csharp
157+
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
158+
var dataFolder = await localFolder.CreateFolderAsync("Data", CreationCollisionOption.OpenIfExists);
159+
var dbPath = System.IO.Path.Combine(dataFolder.Path, "save.db");
160+
var liteDb = new LiteDatabase(dbPath);
161+
```
149162

150163
## LiteDB `async`
151164
One thing you might have noticed until now: All the methods I used are **synchrnous**. There is no `await liteDatabase.GetCollection<TodoItem>().ToListAsync()` or friends. You should consider the asynchrnous paradigm. If you have a regular desktop application or even the WASM head, `async` makes sense, as it doesn't block the UI thread. **LiteDB** itself doesn't offer asynchrnous operations, but there is a community project, which does that: [litedb-async](https://github.com/mlockett42/litedb-async).

0 commit comments

Comments
 (0)