8

Using DataStore With Kotlin Serialization | by Rohit | Android Developers | Apr,...

 3 years ago
source link: https://medium.com/androiddevelopers/using-datastore-with-kotlin-serialization-6552502c5345
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Using DataStore With Kotlin Serialization

Up till now we’ve shared how to use DataStore with Protos or Preferences. Under the hood both DataStore versions use Protos to serialize the data. You can also use DataStore with custom data classes using Kotlin Serialization. This can help reduce boilerplate code without having to learn or depend on the Protobuf library while still providing a schema for your data.

There are a few things you’ll need to do:

  • Define a data class
  • Ensure your data class is immutable
  • Implement a DataStore Serializer using Kotlin Serialization
  • Start using it

Define a data class

Kotlin data classes are great for use with DataStore since they work seamlessly with Kotlin Serialization. DataStore relies on equals and hashCode which are automatically generated for data classes. Data classes also generate toString and copy functions which are useful for debugging and updating data

Ensure your data class is immutable

It is very important to ensure that your class is immutable since DataStore is not compatible with mutable types. Using mutable types with DataStore will result in hard to catch bugs and race conditions. Data classes aren’t necessarily immutable.

Vars are mutable, so instead you should use vals:

Arrays are mutable, so you shouldn’t expose them.

Even if we use the read-only List as a member of our data class, it’s still mutable. Instead you should consider using immutable/persistent collections:

Using mutable types as a member of your data class makes it mutable. Instead, you should ensure that all members are immutable types.

Implement your DataStore Serializer

Kotlin Serialization supports multiple formats including JSON and Protocol buffers. I’ll use JSON here since it’s very common, easy to use, and is stored in cleartext which makes for easy debugging. Protobuf is also a good option since it is smaller, faster and compatible with protobuf-lite.

In order to read and write your data class to JSON using Kotlin Serialization, you need to annotate your data class with @Serializable and use Json.decodeFromString<YourType>(string) and Json.encodeToString(data). Here’s an example with UserPreferences:

⚠️ Parcelables are not safe to use with DataStore because the data format may change between Android versions.

Using the Serializer

Pass the serializer you created into DataStore when constructing it:

Reading data looks the same as it does with protos:

You can use the generated .copy() function to update data:

Conclusion

Using DataStore with Kotlin Serialization and data classes can reduce boiler plate and help simplify your code, however, you must be careful not to introduce bugs through mutability. All you need to do is define your data class and implement the serializer. Try it for yourself!

To learn more about DataStore, check out our documentation and get some hands on experience with our Proto DataStore and Preferences DataStore codelabs.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK