SlideShare a Scribd company logo
Introduction to
Jetpack DataStore
Wonyoung Choi (Toru)
Android Advocate,
Singapore Tourism Board
Introduction
1.Data storage solution in Jetpack Library
2.Aiming at replacing SharedPreferences
3.Released in alpha, in Sep 2020
What is DataStore?
1.Supporting Key-Value pair / typed object
2.Backed by Kotlin Coroutines, and Flow
3.Storing data in asynchronous way
What is DataStore?
Components of DataStore
DataStore
Preference Proto
1.Preference:Key-Value Pair to store data
2.Proto:custom data type with specified
Schema using Protocol Buffers
Components of DataStore
Comparison to SharedPreference
1.Safe to use in UI Thread
2.Robust to RuntimeException when parsing
3.Providing migration
4.Type safety
Highlights on DataStore
Using DataStore
dependencies {
// Preferences DataStore
implementation “androidx.datastore:datastore-preferences:1.0.0-alpha01"
}
For Preference DataStore
plugins {
...
id "com.google.protobuf" version "0.8.12"
}
dependencies {
implementation "androidx.datastore:datastore-core:1.0.0-alpha01"
implementation "com.google.protobuf:protobuf-javalite:3.10.0"
...
}
For ProtoDataStore
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.10.0"
}
generateProtoTasks {
all().each { task ->
task.builtins {
java {
option 'lite'
}
}
}
}
}
For ProtoDataStore
syntax = "proto3";
option java_package = "<your package name here>";
option java_multiple_files = true;
message Settings {
int my_counter = 1;
}
For ProtoDataStore
app/src/main/proto/
filename.proto
Create DataStore
// with Preferences DataStore
val dataStore: DataStore<Preferences> = context.createDataStore(
name = "settings"
)
For Preference DataStore
object SettingsSerializer : Serializer<Settings> {
override fun readFrom(input: InputStream): Settings {
try {
return Settings.parseFrom(input)
} catch (exception: InvalidProtocolBufferException) {
throw CorruptionException("Cannot read proto.", exception)
}
}
override fun writeTo(t: Settings, output: OutputStream) = t.writeTo(output)
}
For ProtoDataStore
// with Proto DataStore
val settingsDataStore: DataStore<Settings> = context.createDataStore(
fileName = "settings.pb",
serializer = SettingsSerializer
)
For ProtoDataStore
Read from DataStore
1.Loading stored data in Flow
2.Retrieving data on Dispatchers.IO
3.Ensuring non-blocking UI thread
What is under the hood of reading DataStore
val MY_COUNTER = preferencesKey<Int>("my_counter")
val myCounterFlow: Flow<Int> = dataStore.data
.map { currentPreferences ->
// Unlike Proto DataStore, there's no type safety here.
currentPreferences[MY_COUNTER] ?: 0
}
For Preference DataStore
val myCounterFlow: Flow<Int> = settingsDataStore.data
.map { settings ->
// The myCounter property is generated for you from your proto schema!
settings.myCounter
}
For ProtoDataStore
Write to DataStore
1.Loading stored data in Flow
2.Retrieving data on Dispatchers.IO
3.Ensuring non-blocking UI thread
Writing to DataStore
suspend fun incrementCounter() {
dataStore.edit { settings ->
// We can safely increment our counter without losing data due to races!
val currentCounterValue = settings[MY_COUNTER] ?: 0
settings[MY_COUNTER] = currentCounterValue + 1
}
}
For Preference DataStore
suspend fun incrementCounter() {
settingsDataStore.updateData { currentSettings ->
// We can safely increment our counter without losing data due to races!
currentSettings.toBuilder()
.setMyCounter(currentSettings.myCounter + 1)
.build()
}
}
For ProtoDataStore
Migration
1.Defining SharedPreferencesMigration
2.Telling DataStore to use migration obj
Migration SharedPreferences to DataStore
val migration = SharedPreferencesMigration(context, "settings_preferences")
val dataStore: DataStore<Preferences> = context.createDataStore(
name = "settings",
migrations = listOf(migration)
)
For Preference DataStore
val migration = SharedPreferencesMigration(
context,
"settings_preferences"
) { sharedPrefs: SharedPreferencesView, currentData: UserPreferences ->
// Map your sharedPrefs to your type here
}
val settingsDataStore: DataStore<Settings> = context.createDataStore(
produceFile = { File(context.filesDir, "settings.preferences_pb") },
serializer = SettingsSerializer,
migrations = listOf(migration)
)
For ProtoDataStore
Furthermore
1.Alpha stage
2.learning curve on what Datastore uses
3.Possibilities of changes in future
Furthermore
Thank you!
TORU0239
TORU_0239

More Related Content

Similar to datastore_devfest2020_incheon

CloudStack Meetup London - Primary Storage Presentation by SolidFire
CloudStack Meetup London - Primary Storage Presentation by SolidFire CloudStack Meetup London - Primary Storage Presentation by SolidFire
CloudStack Meetup London - Primary Storage Presentation by SolidFire
NetApp
 
Practical OData
Practical ODataPractical OData
Practical OData
Vagif Abilov
 
What's new in the world of the Autonomous Database in 2023
What's new in the world of the Autonomous Database in 2023What's new in the world of the Autonomous Database in 2023
What's new in the world of the Autonomous Database in 2023
Sandesh Rao
 
What's new in Autonomous Database - OCYatra2023 - Sandesh Rao.pdf
What's new in Autonomous Database - OCYatra2023 - Sandesh Rao.pdfWhat's new in Autonomous Database - OCYatra2023 - Sandesh Rao.pdf
What's new in Autonomous Database - OCYatra2023 - Sandesh Rao.pdf
Sandesh Rao
 
ДЕНИС КЛЕПIКОВ «Long Term storage for Prometheus» Lviv DevOps Conference 2019
ДЕНИС КЛЕПIКОВ «Long Term storage for Prometheus» Lviv DevOps Conference 2019ДЕНИС КЛЕПIКОВ «Long Term storage for Prometheus» Lviv DevOps Conference 2019
ДЕНИС КЛЕПIКОВ «Long Term storage for Prometheus» Lviv DevOps Conference 2019
UA DevOps Conference
 
Stream Processing in SmartNews #jawsdays
Stream Processing in SmartNews #jawsdaysStream Processing in SmartNews #jawsdays
Stream Processing in SmartNews #jawsdays
SmartNews, Inc.
 
Create generic delta
Create generic deltaCreate generic delta
Create generic delta
Garuda Trainings
 
Paul Lammertsma: Account manager & sync
Paul Lammertsma: Account manager & syncPaul Lammertsma: Account manager & sync
Paul Lammertsma: Account manager & sync
mdevtalk
 
CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara
NetApp
 
Effective Android Data Binding
Effective Android Data BindingEffective Android Data Binding
Effective Android Data Binding
Eric Maxwell
 
The MySQL Performance Schema & New SYS Schema
The MySQL Performance Schema & New SYS SchemaThe MySQL Performance Schema & New SYS Schema
The MySQL Performance Schema & New SYS Schema
Ted Wennmark
 
World2016_T1_S8_How to upgrade your cubes from 9.x to 10 and turn on optimize...
World2016_T1_S8_How to upgrade your cubes from 9.x to 10 and turn on optimize...World2016_T1_S8_How to upgrade your cubes from 9.x to 10 and turn on optimize...
World2016_T1_S8_How to upgrade your cubes from 9.x to 10 and turn on optimize...Karthik K Iyengar
 
Java API for WebSocket 1.0: Java EE 7 and GlassFish
Java API for WebSocket 1.0: Java EE 7 and GlassFishJava API for WebSocket 1.0: Java EE 7 and GlassFish
Java API for WebSocket 1.0: Java EE 7 and GlassFish
Arun Gupta
 
Storage Plug-ins
Storage Plug-ins Storage Plug-ins
Storage Plug-ins buildacloud
 
Accelerated data access
Accelerated data accessAccelerated data access
Accelerated data access
gordonyorke
 
PPT_CC.pptx
PPT_CC.pptxPPT_CC.pptx
PPT_CC.pptx
Ajit Mali
 
EE5111 a0195042 j_iot_project_report_update
EE5111 a0195042 j_iot_project_report_updateEE5111 a0195042 j_iot_project_report_update
EE5111 a0195042 j_iot_project_report_update
JingmingPeng1
 
Apache stratos hangout 3
Apache stratos hangout   3Apache stratos hangout   3
Apache stratos hangout 3Nirmal Fernando
 

Similar to datastore_devfest2020_incheon (20)

CloudStack Meetup London - Primary Storage Presentation by SolidFire
CloudStack Meetup London - Primary Storage Presentation by SolidFire CloudStack Meetup London - Primary Storage Presentation by SolidFire
CloudStack Meetup London - Primary Storage Presentation by SolidFire
 
Practical OData
Practical ODataPractical OData
Practical OData
 
What's new in the world of the Autonomous Database in 2023
What's new in the world of the Autonomous Database in 2023What's new in the world of the Autonomous Database in 2023
What's new in the world of the Autonomous Database in 2023
 
What's new in Autonomous Database - OCYatra2023 - Sandesh Rao.pdf
What's new in Autonomous Database - OCYatra2023 - Sandesh Rao.pdfWhat's new in Autonomous Database - OCYatra2023 - Sandesh Rao.pdf
What's new in Autonomous Database - OCYatra2023 - Sandesh Rao.pdf
 
ДЕНИС КЛЕПIКОВ «Long Term storage for Prometheus» Lviv DevOps Conference 2019
ДЕНИС КЛЕПIКОВ «Long Term storage for Prometheus» Lviv DevOps Conference 2019ДЕНИС КЛЕПIКОВ «Long Term storage for Prometheus» Lviv DevOps Conference 2019
ДЕНИС КЛЕПIКОВ «Long Term storage for Prometheus» Lviv DevOps Conference 2019
 
Stream Processing in SmartNews #jawsdays
Stream Processing in SmartNews #jawsdaysStream Processing in SmartNews #jawsdays
Stream Processing in SmartNews #jawsdays
 
Create generic delta
Create generic deltaCreate generic delta
Create generic delta
 
ADO.NET
ADO.NETADO.NET
ADO.NET
 
Paul Lammertsma: Account manager & sync
Paul Lammertsma: Account manager & syncPaul Lammertsma: Account manager & sync
Paul Lammertsma: Account manager & sync
 
CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara
 
Data load utility
Data load utilityData load utility
Data load utility
 
Effective Android Data Binding
Effective Android Data BindingEffective Android Data Binding
Effective Android Data Binding
 
The MySQL Performance Schema & New SYS Schema
The MySQL Performance Schema & New SYS SchemaThe MySQL Performance Schema & New SYS Schema
The MySQL Performance Schema & New SYS Schema
 
World2016_T1_S8_How to upgrade your cubes from 9.x to 10 and turn on optimize...
World2016_T1_S8_How to upgrade your cubes from 9.x to 10 and turn on optimize...World2016_T1_S8_How to upgrade your cubes from 9.x to 10 and turn on optimize...
World2016_T1_S8_How to upgrade your cubes from 9.x to 10 and turn on optimize...
 
Java API for WebSocket 1.0: Java EE 7 and GlassFish
Java API for WebSocket 1.0: Java EE 7 and GlassFishJava API for WebSocket 1.0: Java EE 7 and GlassFish
Java API for WebSocket 1.0: Java EE 7 and GlassFish
 
Storage Plug-ins
Storage Plug-ins Storage Plug-ins
Storage Plug-ins
 
Accelerated data access
Accelerated data accessAccelerated data access
Accelerated data access
 
PPT_CC.pptx
PPT_CC.pptxPPT_CC.pptx
PPT_CC.pptx
 
EE5111 a0195042 j_iot_project_report_update
EE5111 a0195042 j_iot_project_report_updateEE5111 a0195042 j_iot_project_report_update
EE5111 a0195042 j_iot_project_report_update
 
Apache stratos hangout 3
Apache stratos hangout   3Apache stratos hangout   3
Apache stratos hangout 3
 

More from Toru Wonyoung Choi

Glancing essential features of Dart, before stepping into Flutter
Glancing essential features of Dart, before stepping into FlutterGlancing essential features of Dart, before stepping into Flutter
Glancing essential features of Dart, before stepping into Flutter
Toru Wonyoung Choi
 
The use case of a scalable architecture
The use case of a scalable architectureThe use case of a scalable architecture
The use case of a scalable architecture
Toru Wonyoung Choi
 
Building an app with Google's new suites
Building an app with Google's new suitesBuilding an app with Google's new suites
Building an app with Google's new suites
Toru Wonyoung Choi
 
Slide_Concat_adapter_july_2020
Slide_Concat_adapter_july_2020Slide_Concat_adapter_july_2020
Slide_Concat_adapter_july_2020
Toru Wonyoung Choi
 
activity_and_fragment_may_2020_lakopi
activity_and_fragment_may_2020_lakopiactivity_and_fragment_may_2020_lakopi
activity_and_fragment_may_2020_lakopi
Toru Wonyoung Choi
 
camera_x_beyond_alpha
camera_x_beyond_alphacamera_x_beyond_alpha
camera_x_beyond_alpha
Toru Wonyoung Choi
 
Slide_For_GDGKL_devfest_2019
Slide_For_GDGKL_devfest_2019Slide_For_GDGKL_devfest_2019
Slide_For_GDGKL_devfest_2019
Toru Wonyoung Choi
 
CameraX, MLKit and AutoML at DevFest Songdo 2019
CameraX, MLKit and AutoML at DevFest Songdo 2019CameraX, MLKit and AutoML at DevFest Songdo 2019
CameraX, MLKit and AutoML at DevFest Songdo 2019
Toru Wonyoung Choi
 
CameraX, MLKit and AutoML at DevFest Cebu 2019
CameraX, MLKit and AutoML at DevFest Cebu 2019CameraX, MLKit and AutoML at DevFest Cebu 2019
CameraX, MLKit and AutoML at DevFest Cebu 2019
Toru Wonyoung Choi
 

More from Toru Wonyoung Choi (9)

Glancing essential features of Dart, before stepping into Flutter
Glancing essential features of Dart, before stepping into FlutterGlancing essential features of Dart, before stepping into Flutter
Glancing essential features of Dart, before stepping into Flutter
 
The use case of a scalable architecture
The use case of a scalable architectureThe use case of a scalable architecture
The use case of a scalable architecture
 
Building an app with Google's new suites
Building an app with Google's new suitesBuilding an app with Google's new suites
Building an app with Google's new suites
 
Slide_Concat_adapter_july_2020
Slide_Concat_adapter_july_2020Slide_Concat_adapter_july_2020
Slide_Concat_adapter_july_2020
 
activity_and_fragment_may_2020_lakopi
activity_and_fragment_may_2020_lakopiactivity_and_fragment_may_2020_lakopi
activity_and_fragment_may_2020_lakopi
 
camera_x_beyond_alpha
camera_x_beyond_alphacamera_x_beyond_alpha
camera_x_beyond_alpha
 
Slide_For_GDGKL_devfest_2019
Slide_For_GDGKL_devfest_2019Slide_For_GDGKL_devfest_2019
Slide_For_GDGKL_devfest_2019
 
CameraX, MLKit and AutoML at DevFest Songdo 2019
CameraX, MLKit and AutoML at DevFest Songdo 2019CameraX, MLKit and AutoML at DevFest Songdo 2019
CameraX, MLKit and AutoML at DevFest Songdo 2019
 
CameraX, MLKit and AutoML at DevFest Cebu 2019
CameraX, MLKit and AutoML at DevFest Cebu 2019CameraX, MLKit and AutoML at DevFest Cebu 2019
CameraX, MLKit and AutoML at DevFest Cebu 2019
 

datastore_devfest2020_incheon

  • 1. Introduction to Jetpack DataStore Wonyoung Choi (Toru) Android Advocate, Singapore Tourism Board
  • 3. 1.Data storage solution in Jetpack Library 2.Aiming at replacing SharedPreferences 3.Released in alpha, in Sep 2020 What is DataStore?
  • 4. 1.Supporting Key-Value pair / typed object 2.Backed by Kotlin Coroutines, and Flow 3.Storing data in asynchronous way What is DataStore?
  • 6. 1.Preference:Key-Value Pair to store data 2.Proto:custom data type with specified Schema using Protocol Buffers Components of DataStore
  • 8.
  • 9. 1.Safe to use in UI Thread 2.Robust to RuntimeException when parsing 3.Providing migration 4.Type safety Highlights on DataStore
  • 11. dependencies { // Preferences DataStore implementation “androidx.datastore:datastore-preferences:1.0.0-alpha01" } For Preference DataStore
  • 12. plugins { ... id "com.google.protobuf" version "0.8.12" } dependencies { implementation "androidx.datastore:datastore-core:1.0.0-alpha01" implementation "com.google.protobuf:protobuf-javalite:3.10.0" ... } For ProtoDataStore
  • 13. protobuf { protoc { artifact = "com.google.protobuf:protoc:3.10.0" } generateProtoTasks { all().each { task -> task.builtins { java { option 'lite' } } } } } For ProtoDataStore
  • 14. syntax = "proto3"; option java_package = "<your package name here>"; option java_multiple_files = true; message Settings { int my_counter = 1; } For ProtoDataStore app/src/main/proto/ filename.proto
  • 16. // with Preferences DataStore val dataStore: DataStore<Preferences> = context.createDataStore( name = "settings" ) For Preference DataStore
  • 17. object SettingsSerializer : Serializer<Settings> { override fun readFrom(input: InputStream): Settings { try { return Settings.parseFrom(input) } catch (exception: InvalidProtocolBufferException) { throw CorruptionException("Cannot read proto.", exception) } } override fun writeTo(t: Settings, output: OutputStream) = t.writeTo(output) } For ProtoDataStore
  • 18. // with Proto DataStore val settingsDataStore: DataStore<Settings> = context.createDataStore( fileName = "settings.pb", serializer = SettingsSerializer ) For ProtoDataStore
  • 20. 1.Loading stored data in Flow 2.Retrieving data on Dispatchers.IO 3.Ensuring non-blocking UI thread What is under the hood of reading DataStore
  • 21. val MY_COUNTER = preferencesKey<Int>("my_counter") val myCounterFlow: Flow<Int> = dataStore.data .map { currentPreferences -> // Unlike Proto DataStore, there's no type safety here. currentPreferences[MY_COUNTER] ?: 0 } For Preference DataStore
  • 22. val myCounterFlow: Flow<Int> = settingsDataStore.data .map { settings -> // The myCounter property is generated for you from your proto schema! settings.myCounter } For ProtoDataStore
  • 24. 1.Loading stored data in Flow 2.Retrieving data on Dispatchers.IO 3.Ensuring non-blocking UI thread Writing to DataStore
  • 25. suspend fun incrementCounter() { dataStore.edit { settings -> // We can safely increment our counter without losing data due to races! val currentCounterValue = settings[MY_COUNTER] ?: 0 settings[MY_COUNTER] = currentCounterValue + 1 } } For Preference DataStore
  • 26. suspend fun incrementCounter() { settingsDataStore.updateData { currentSettings -> // We can safely increment our counter without losing data due to races! currentSettings.toBuilder() .setMyCounter(currentSettings.myCounter + 1) .build() } } For ProtoDataStore
  • 28. 1.Defining SharedPreferencesMigration 2.Telling DataStore to use migration obj Migration SharedPreferences to DataStore
  • 29. val migration = SharedPreferencesMigration(context, "settings_preferences") val dataStore: DataStore<Preferences> = context.createDataStore( name = "settings", migrations = listOf(migration) ) For Preference DataStore
  • 30. val migration = SharedPreferencesMigration( context, "settings_preferences" ) { sharedPrefs: SharedPreferencesView, currentData: UserPreferences -> // Map your sharedPrefs to your type here } val settingsDataStore: DataStore<Settings> = context.createDataStore( produceFile = { File(context.filesDir, "settings.preferences_pb") }, serializer = SettingsSerializer, migrations = listOf(migration) ) For ProtoDataStore
  • 32. 1.Alpha stage 2.learning curve on what Datastore uses 3.Possibilities of changes in future Furthermore