{"id":131,"date":"2023-12-20T11:49:23","date_gmt":"2023-12-20T06:19:23","guid":{"rendered":"https:\/\/zerofold.in\/blogs\/?p=131"},"modified":"2023-12-21T12:33:09","modified_gmt":"2023-12-21T07:03:09","slug":"implementing-data-store-in-android-project","status":"publish","type":"post","link":"https:\/\/zerofold.in\/blogs\/blog\/2023\/12\/20\/implementing-data-store-in-android-project\/","title":{"rendered":"Implementing Data Store in Android Project"},"content":{"rendered":"\n<p>Official Google Android Developer documentation encourages using <code>DataStore<\/code>, if you already have implemented <code>SharedPreferences<\/code>.<\/p>\n\n\n\n<p>It is recommended to not store complex data objects in <code>SharedPreferences<\/code>, for complex types, consider using Room Database. But, if you still wish to procced using <code>DataStore<\/code> for storing complex data objects, you can do it via Proto Data Store.<\/p>\n\n\n\n<p>Add Preferences <code>DataStore<\/code> to your project, by adding the following line in your Gradle file:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"border-width:1px;font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.042), 0.9rem);\"><code>dependencies {\nimplementation(\"androidx.datastore:datastore-preferences:1.0.0\")\n}<\/code><\/pre>\n\n\n\n<p>Creating a Preferences <code>DataStore<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"border-width:1px;font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.042), 0.9rem);\"><code>val Context.appPreferences: DataStore&lt;Preferences> by preferencesDataStore(\"appDataStore\")<\/code><\/pre>\n\n\n\n<p>You can store data in your Data Store using Key\/Value pair method. Since, we are using Preferences Data Store, we have to stick storing and retrieving a primitive data type such as Int or Double, or String which isn&#8217;t Primitive but not complex either. Here is how we read from Preferences Data Store:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"border-width:1px;font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.042), 0.9rem);\"><code>val getLastStoredString: Flow&lt;String> = context.appPreferences.data.map { preferences ->\rpreferences&#91;\"LAST_STORED_STRING\"] ?: \"\"\r}<\/code><\/pre>\n\n\n\n<p>Notice how there is no type safety while retrieving the String value, we have to provide an empty string if there is no <code>LAST_STORED_STRING<\/code> value present.<\/p>\n\n\n\n<p>Here is how you can write to Preferences Data Store:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"border-width:1px;font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.042), 0.9rem);\"><code>suspend fun setLastStoredString(stringToSave: String){\r\ncontext.appPreferences.edit { preferences ->\r\npreferences&#91;\"LAST_STORED_STRING\"] = stringToSave\n   }\r\n}<\/code><\/pre>\n\n\n\n<p>To store small values, Preferences Data Store is a great solution. The most real-time used application of Data Store is to set and get the default language of an application. We&#8217;ll see it in working in next part.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Official Google Android Developer documentation encourages using DataStore, if you already have implemented SharedPreferences. It is recommended to not store complex data objects in SharedPreferences, for complex types, consider using Room Database. But, if you still wish to procced using DataStore for storing complex data objects, you can do it via Proto Data Store. Add [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10,59,9],"tags":[5,14,53,7,57,58,56,54],"class_list":["post-131","post","type-post","status-publish","format-standard","hentry","category-android","category-data-store","category-mobile","tag-android","tag-data","tag-datastore","tag-mobile","tag-preferences","tag-proto","tag-shared","tag-store"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/posts\/131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/comments?post=131"}],"version-history":[{"count":7,"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/posts\/131\/revisions"}],"predecessor-version":[{"id":138,"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/posts\/131\/revisions\/138"}],"wp:attachment":[{"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/media?parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/categories?post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zerofold.in\/blogs\/wp-json\/wp\/v2\/tags?post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}