You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: learn-pr/dot-net-maui/store-local-data/2-compare-storage-options.yml
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -4,8 +4,8 @@ title: Compare storage options
4
4
metadata:
5
5
unitType: unit
6
6
title: "Compare storage options"
7
-
description: "Describe the options available for storing data locally in a .NET MAUI application"
8
-
ms.date: 10/13/2023
7
+
description: "Describe the options available for storing data locally in a .NET Multi-platform App UI (MAUI) application"
8
+
ms.date: 11/11/2024
9
9
author: codemillmatt
10
10
ms.author: masoucou
11
11
ms.topic: unit
@@ -23,15 +23,15 @@ quiz:
23
23
explanation: "No. The App Sandbox doesn't include a Music folder by default. Instead you need to use a system provided folder."
24
24
- content: "Documents"
25
25
isCorrect: true
26
-
explanation: "This is the correct answer. You use the Documents folder to store user generated content."
26
+
explanation: "Correct. You use the Documents folder to store user generated content."
27
27
- content: "Library"
28
28
isCorrect: false
29
29
explanation: "No. The Library folder is designed to store app-generated data. The music content is data that the user specifically asks to save. You would use the Library folder for files created without any direct action from the user. Use the Documents folder for this scenario."
30
30
- content: "Suppose you want to store a small piece of data so that it's accessible between application launches. Which data storage option is the most appropriate?"
31
31
choices:
32
32
- content: "Preferences."
33
33
isCorrect: true
34
-
explanation: "This is the correct answer. Preferences are used to store data as key-value pairs. This location is a great place for simple bits of data."
34
+
explanation: "Correct. Preferences are used to store data as key-value pairs. This location is a great place for simple bits of data."
35
35
- content: "File system"
36
36
isCorrect: false
37
37
explanation: "No. Use the file system to store raw files like XML or text files. For this scenario, use Preferences."
Copy file name to clipboardExpand all lines: learn-pr/dot-net-maui/store-local-data/includes/1-introduction.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,20 @@
1
-
When you're building mobile applications, it's common to store data locally on a device for performance reasons. .NET MAUI allows you to cache data locally on the device on which the application is running. You can store this data using several different technologies. To store relational data, you can use a SQLite database.
1
+
When you're building mobile applications, it's common to store data locally on a device for performance reasons. .NET Multi-platform App UI (MAUI) allows you to cache data locally on the device on which the application is running. You can store this data using several different technologies. To store relational data, you can use a SQLite database.
2
2
3
3
Suppose you're building a social-media application that lets users connect with each other. Because the data about users is stored remotely, you have to call the REST endpoint to get information about the users with which you're connected every time you restart the application.
4
4
5
5
This design works. However, it's inefficient, because you have to keep downloading the user information from the server every time you restart the application.
6
6
7
7
A more efficiently designed app downloads the user data once and saves it locally to the device. With this design, you only have to download information for other users when you want to make a new connection. You can create this more efficient design by using a local database.
8
8
9
-
In this module, you'll build a .NET MAUI application that stores data locally in a database. You'll start by exploring the different data-storage options that are available.
9
+
In this module, you build a .NET MAUI application that stores data locally in a database. You start by exploring the different data-storage options that are available.
10
10
11
-
Next, you'll look at SQLite and learn how to insert and retrieve data. Finally, you'll use the asynchronous methods available in SQLite to interact with the database without blocking the UI.
11
+
Next, you look at SQLite and learn how to insert and retrieve data. Finally, you use the asynchronous methods available in SQLite to interact with the database without blocking the UI.
12
12
13
-
By the end of this module, you'll be able to build a .NET MAUI application that stores data locally in a SQLite database.
13
+
By the end of this module, you're able to build a .NET MAUI application that stores data locally in a SQLite database.
14
14
15
15
## Learning objectives
16
16
17
-
In this module, you'll learn how to:
17
+
In this module, you learn how to:
18
18
19
19
- Compare different data storage options that are available for .NET MAUI applications.
Copy file name to clipboardExpand all lines: learn-pr/dot-net-maui/store-local-data/includes/2-compare-storage-options.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
-
.NET MAUI provides multiple storage options for caching data locally on a device, depending on the nature, structure, and size of the data. The three most commonly used options for storing data locally in a .NET MAUI app are:
1
+
.NET Multi-platform App UI (MAUI) provides multiple storage options for caching data locally on a device, depending on the nature, structure, and size of the data. The three most commonly used options for storing data locally in a .NET MAUI app are:
2
2
3
3
-**Preferences**: Stores data in key-value pairs
4
4
-**File system**: Stores loose files directly on the device through file-system access
5
5
-**Database**: Stores data in a relational database
6
6
7
7
:::image type="content" source="../media/2-data-storage-options.svg" alt-text="Diagram of the common local storage options available to a .NET MAUI app.":::
8
8
9
-
In this unit, you'll look at these storage options and the situations for which each option is most appropriate.
9
+
In this unit, we look at these storage options and the situations for which each option is most appropriate.
When you're working with loose files such XML files, you need to store them in a suitable location in the file system. Some of this data might be sensitive, and you don't want to save it to a location where other apps or users could easily access it. .NET MAUI apps provide the **app sandbox**. The app sandbox is a private area with which your application can work. By default, no other applications can access this area other than the operating system. You can access the sandbox by using the `AppDataDirectory` static property of the `FileSystem` class:
54
+
When you're working with loose files such as XML files, you need to store them in a suitable location in the file system. Some of this data might be sensitive, and you don't want to save it to a location where other apps or users could easily access it. .NET MAUI apps provide the **app sandbox**. The app sandbox is a private area with which your application can work. By default, no other applications can access this area other than the operating system. You can access the sandbox by using the `AppDataDirectory` static property of the `FileSystem` class:
55
55
56
56
```csharp
57
57
stringpath=FileSystem.AppDataDirectory;
58
58
```
59
59
60
-
In this code, the `path` variable contains the file path to the location where you can store files for the application to use. You can read and write data to files in this folder using the techniques shown in the *When to use the file system* section.
60
+
In this code, the `path` variable contains the file path to the location where you can store files for the application to use. You can read and write data to files in this folder, by using the techniques shown in the *When to use the file system* section.
61
61
62
62
> [!NOTE]
63
63
> The `FileSystem.AppDataDirectory` property is an abstraction of a device-specific path; it evaluates to different folders on Android, iOS, and WinUI3. This abstraction allows you to write code that references the sandbox in a manner that's independent of the platform on which it runs. Use this abstraction rather than referencing a device-specific path explicitly in your code.
@@ -66,7 +66,7 @@ In this code, the `path` variable contains the file path to the location where y
66
66
67
67
Apple has iOS guidelines for where files should be stored. There are two main folder locations:
68
68
69
-
-**The *Library* folder**: This folder is returned by the `FileSystem.AppDataDirectory` property, as described previously. Use the Library folder when you're storing data that's app-generated.
69
+
-**The *Library* folder**: As described previously, the `FileSystem.AppDataDirectory` property returns this folder. Use the Library folder when you're storing app-generated data.
70
70
71
71
-**The *Documents* folder**: The following code snippet shows how to reference this folder in the `docFolder` variable. Use the Documents folder to store user-generated data only. This data is created in direct response to a user action. For example, if you were creating a text-editing application such as Microsoft Word, you'd store the document in the Documents folder.
Copy file name to clipboardExpand all lines: learn-pr/dot-net-maui/store-local-data/includes/3-store-data-locally-with-sqlite.md
+22-22Lines changed: 22 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
SQLite is useful when you have relational data. Suppose you're building a social-media app. You need to store information about subscribers to the app. This data includes a unique ID for each user and their name. You can easily model this kind of relationship in a SQLite database.
2
2
3
-
In this unit, you'll learn how to use SQLite in a .NET MAUI application by using SQLite-net.
3
+
In this unit, you learn how to use SQLite in a .NET MAUI application by using SQLite-net.
4
4
5
5
## What is SQLite?
6
6
7
-
SQLite is a lightweight cross-platform local database that's become an industry standard for mobile applications. SQLite doesn't require a server. The database is stored in a single disk file on the device's file system. All read and write operations are run directly against the SQLite disk file.
7
+
SQLite is a lightweight cross-platform local database that's an industry standard for mobile applications. SQLite doesn't require a server. The database is stored in a single disk file on the device's file system. All read and write operations are run directly against the SQLite disk file.
8
8
9
9
The SQLite native libraries are built into Android and iOS by default; however, the engine only supports a C/C++ API. This scenario isn't ideal for .NET developers, who want some way for SQLite and .NET to interact.
10
10
@@ -33,7 +33,7 @@ SQLite-net is shipped as a NuGet package. You must add the **sqlite-net-pcl** pa
33
33
34
34
## How to connect to a SQLite database
35
35
36
-
You can establish a connection to a SQLite database from an app through a `SQLiteConnection` object. This class is defined in the `SQLite` namespace, together with the other types and methods SQLite provides. When you instantiate this object, you pass in the filename for the database file. The constructor will then either open the file if it exists or create it if it isn't present.
36
+
You can establish a connection to a SQLite database from an app through a `SQLiteConnection` object. This class is defined in the `SQLite` namespace, together with the other types and methods SQLite provides. When you instantiate this object, you pass in the filename for the database file. The constructor then either opens the file if it exists or creates it if it isn't present.
37
37
38
38
The following code shows an example:
39
39
@@ -48,16 +48,16 @@ Remember that `filename` should point to a location in the app sandbox.
48
48
49
49
## How to create a table
50
50
51
-
Recall that SQLite-net is an object-relational mapper, which means you can build your database schema from C# classes. SQLite-net can build a database table from an ordinary C# class, but there are many attributes that you can add to a class to provide additional metadata. This metadata helps SQLite to enforce features such as uniqueness and apply constraints to your data.
51
+
Recall that SQLite-net is an object-relational mapper, which means you can build your database schema from C# classes. SQLite-net can build a database table from an ordinary C# class, but there are many attributes that you can add to a class to provide more metadata. This metadata helps SQLite to enforce features such as uniqueness and apply constraints to your data.
52
52
53
-
The attributes available include:
53
+
The available attributes include:
54
54
55
-
-**`Table`**: Specify the name of the table if you want it to be something other than the class's name
56
-
-**`PrimaryKey`**: Specify that a column is the primary key
57
-
-**`AutoIncrement`**: Specify that a column should automatically increase in value when a new row is inserted
58
-
-**`Column`**: Specify the name of a column if you want it to be something other than the property name
59
-
-**`MaxLength`**: Specify the maximum number of characters that can be used in the column
60
-
-**`Unique`**: Specify that the value in the column must be unique from all other rows
55
+
-**`Table`**: Specify the name of the table if you want it to be something other than the class's name.
56
+
-**`PrimaryKey`**: Specify that a column is the primary key.
57
+
-**`AutoIncrement`**: Specify that a column should automatically increase in value when a new row is inserted.
58
+
-**`Column`**: Specify the name of a column if you want it to be something other than the property name.
59
+
-**`MaxLength`**: Specify the maximum number of characters that can be used in the column.
60
+
-**`Unique`**: Specify that the value in the column must be unique from all other rows.
61
61
62
62
The following code shows an updated version of the `User` class that applies these attributes:
63
63
@@ -116,17 +116,17 @@ The `Table` method retrieves all the rows from a table. On most occasions, you w
116
116
117
117
SQLite-net supports many common LINQ queries including:
118
118
119
-
- Where
120
-
- Take
121
-
- Skip
122
-
- OrderBy
123
-
- OrderByDescending
124
-
- ThenBy
125
-
- ElementAt
126
-
- First
127
-
- FirstOrDefault
128
-
- ThenByDescending
129
-
- Count
119
+
-`Where`
120
+
-`Take`
121
+
-`Skip`
122
+
-`OrderBy`
123
+
-`OrderByDescending`
124
+
-`ThenBy`
125
+
-`ElementAt`
126
+
-`First`
127
+
-`FirstOrDefault`
128
+
-`ThenByDescending`
129
+
-`Count`
130
130
131
131
With these methods, you can use the extension method syntax or the LINQ C# syntax. For example, here's a snippet of code that enables you to retrieve the details of a specified user:
0 commit comments