When a factory delegate completes, the lazy-initialized value is available, and any methods awaiting the AsyncLazy instance receive the value. Factory delegates will run on a thread pool thread, and will not be executed more than once (even when multiple threads attempt to start them simultaneously). The factory delegate that's passed to the constructor can either be synchronous or asynchronous. The AsyncLazy class combines the Lazy and Task types to create a lazy-initialized task that represents the initialization of a resource. Instance = new Lazy>(() => Task.Run(factory)) In order to start the database initialization, avoid blocking execution, and have the opportunity to catch exceptions, the sample application uses asynchronous lazy initalization, represented by the AsyncLazy class: public class AsyncLazy Using a persistent, static connection offers better performance than opening and closing connections multiple times during a single app session. The database connection is a static field which ensures that a single database connection is used for the life of the app. In addition, any await operations after the construction is complete continue immediately since the value is available. Then, when the construction completes, all await operations complete. ![]() If multiple threads attempt to access the field simultaneously, they will all use the single construction. The Instance field, of type AsyncLazy is constructed the first time it's awaited. ![]() The Instance field is used to create the database table for the TodoItem object, if it doesn't already exist, and returns a TodoItemDatabase as a singleton. Public static readonly AsyncLazy Instance = new AsyncLazy(async () =>ĬreateTableResult result = await Database.CreateTableAsync() ĭatabase = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags) The TodoItemDatabase uses asynchronous lazy initialization, represented by the custom AsyncLazy class, to delay initialization of the database until it's first accessed: public class TodoItemDatabase The Todo app defines a TodoItemDatabase class for this purpose. This class centralizes query logic and simplifies the management of database initialization, making it easier to refactor or expand data operations as the app grows. Create a database access classĪ database wrapper class abstracts the data access layer from the rest of the app. For more information about SQLiteOpenFlags, see Opening A New Database Connection on. ![]() You may need to specify different flags depending on how your database will be used. ProtectionNone: The database file isn't encrypted.ProtectionCompleteUntilFirstUserAuthentication: The file is encrypted until after the user has booted and unlocked the device.ProtectionCompleteUnlessOpen: The file is encrypted until it's opened but is then accessible even if the user locks the device.ProtectionComplete: The file is encrypted and inaccessible while the device is locked.SharedCache: The connection will participate in the shared cache, if it's enabled.ReadWrite: The connection can read and write data.PrivateCache: The connection will not participate in the shared cache, even if it's enabled.NoMutex: The connection is opened in multi-threading mode.FullMutex: The connection is opened in serialized threading mode.Create: The connection will automatically create the database file if it doesn't exist.The SQLiteOpenFlag enum supports these values: The constants file specifies default SQLiteOpenFlag enum values that are used to initialize the database connection. Return Path.Combine(basePath, DatabaseFilename) Var basePath = Environment.GetFolderPath() create the database if it doesn't exist ![]() Public const SQLite.SQLiteOpenFlags Flags = Public const string DatabaseFilename = "TodoSQLite.db3" The sample project includes a Constants.cs file that provides common configuration data: public static class Constants SQLite.NET is a third-party library that's supported from the praeclarum/sqlite-net repo.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |