

Data classes are not necessarily immutable. Using mutable types in conjunction with datastores can lead to hard-to-catch errors and race conditions. It is important to ensure that your data classes are immutable, because DataStore is not compatible with mutable types. val sortOrder: SortOrderĬopy the code Make sure your data classes are immutable * Copyright 2021 Google LLC.spdx-license-Identifier: Apache-2.0 */ data class UserPreferences(

Data classes also generate toString and copy functions that are easy to debug and update data.

DataStore relies on equals and hashCode, which are automatically generated by the data class. The Kotlin data classes are ideal for use with DataStore because they work seamlessly with the Kotlin serialization. Implement DataStore serializer using Kotlin serialization.Make sure your data classes are immutable.You need to complete the following operations: This helps reduce boilerplate code and does not require learning or relying on the Protobuf library while still providing architecture for the data. You can also use Kotlin serialization, combining DataStore with custom data classes. Both DataStore versions serialize data behind the scenes using Protos. We've previously shared how to use the Proto DataStore and Preferences DataStore.
