Skip to content

Commit 7f4d70b

Browse files
committed
Add documentation for datastore
1 parent dd998c1 commit 7f4d70b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
label: DataStore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
**STORING PREFERENCES DATA**
2+
3+
Fhircore uses Preferences [DataStore](https://developer.android.com/topic/libraries/architecture/datastore) and Proto DataStore to store preferences data. Primitive types are stored in the Preferences DataStore and structured types are stored in the Proto DataStore.
4+
5+
The interfaces exposing access to these two storage options are located in ```engine/datastore/PreferenceDataStore``` and ```engine/datastore/ProtoDataStore```.
6+
7+
***PreferenceDataStore.kt***<br/>
8+
To register a key-value pair that you intend to store in the preferences datastore, add a key that enforces the type of data being stored and the name of the key in the Keys companion object.
9+
10+
The read() method returns a flow. To observe the flow, call the function in the view model and store the resulting flow. You may then convert it to a StateFlow and expose it to the UI.
11+
12+
The write() method can be used in the UI directly but it is preferable to expose it through a view model to follow the MVVM pattern.
13+
14+
Both read() and write() are Generic methods.
15+
16+
***ProtoDataStore.kt***<br/>
17+
Proto DataStore allows us to store objects in a type-safe way. For ease of adaptation, we use [Kotlinx serialization](https://kotlinlang.org/docs/serialization.html#serialize-and-deserialize-json) to allow us to use Kotlin data classes instead of Protobuf files to define the schema of the data being stored.
18+
19+
Preferably, any objects made within fhircore with the intent of being persisted in the Proto DataStore should be annotated with ```@Serializable``` (kotlinx.serialization.Serializable).
20+
21+
You also need to register a serializer that will serialize and deserialize the object stored within a JSON file on the device. The serializers are found within ```engine/datastore/serializers```

0 commit comments

Comments
 (0)