Intermediate: Data management (Lightweight Preference Database) in Harmony OS

Lokesh Suryan
5 min readJul 9, 2021

--

Introduction

Huawei provides various services for developers to make ease of development and provides best user experience to end users. In this article, we will cover Lightweight Preference Database with Java in Harmony OS.

Lightweight Preference database is used to retrieve and store small amount of data. In java, string, integer, long, number etc. are considered as primitive data type. As the stored data is already loaded in the memory, the faster data access speed achieves a higher work efficiency. Lightweight preferences database are used to store data in key and value pair so that we can retrieve the value on the basis of key.

Basic Concepts

  • Key-value database

A database that stores data in key-value pairs. It works in a similar way to the Java map. The key indicates keyword, and value indicates corresponding value.

  • Non-relational database

A database not in compliance with the atomicity, consistency, isolation, and durability (ACID) database management properties of relational data transactions. Instead, the data in a non-relational database is independent and scalable.

  • Preference data

A type of data that is frequently accessed and used.

Limitations and Constraints

  • A key should be a string with a maximum of 80 characters. A key cannot be an empty string.
  • A value in the format of string can have a maximum of 8192 characters. A value can be an empty string.
  • To avoid a high memory cost, it is recommended that the lightweight preference database store is not more than ten thousand data entries.

When to Use

The lightweight preference database suits the storage of lightweight and frequently used data better than the storage of a large amount data or data with frequent changes. The data in the lightweight preference database can be persistently stored on a device in the form of files. The accessed preferences instance contains all the data of the files, and is always loaded to the memory of the device. You can operate the lightweight preference database by calling the related APIs.

Development Overview

You need to install DevEcho studio IDE and I assume that you have prior knowledge about the Harmony OS and java.

Hardware Requirements

  • A computer (desktop or laptop) running Windows 10.
  • A Huawei phone (with the USB cable), which is used for debugging.

Software Requirements

  • Java JDK installation package.
  • DevEcho studio installed.

Follows the steps.

1. Create Harmony Project.

  • Open DevEcho studio.
  • Click NEW Project, select a Project Templet.
  • Select ability template and click Next as per below image.

Enter Project and Package Name and click on Finish.

2. Once you have created the project, DevEco Studio will automatically sync it with Gradle files. Find the below image after synchronization is successful.

3. Update Permission and app version in config.json file as per your requirement, otherwise retain the default values.

4. Create New Ability, as follows.

5. Development Procedure.

Add the below code in MainAbilitySlice.java

Create a new ability and Add the below code in WriteData.java

Create a layout file Add the below code in write_data_ability.xml

6. To build apk and run in device, choose Build > Generate Key and CSR Build for Hap(s)\ APP(s) or Build and Run into connected device, follow the steps.

Result

  1. Click on UI Store data in preferences button. It will navigate to write data screen as per below images.

Pros: Preferences works on a Key-Value basis. You simply provide the Key and get back the Value you stored are great.

Cons: Preferences is convenient to store small bits of data only. You should not store large amounts of data in Preferences.

2. Enter data and click on “Store data in preferences” button. It will store data in preference as per below images.

3. Click on “Read data”button, we will read data from preferences as per below images.

Pros: If you want to map simple values (like int, boolean, String) then Preferences is better option.

Cons: Regarding speed and efficiency is low for large data.

Tips and Tricks

  • Always use the latest version of DevEcho Studio.
  • Use Harmony Device Simulator from HVD section.

Conclusion

In this article, we have learnt Data Management in Harmony OS. Lightweight Preference database is used to retrieve and store small amount of data. In java, string, integer, long, number etc. are considered as primitive data type. As the stored data is already loaded in the memory, the faster data access speed achieves a higher work.

Thanks for reading the article, please do like and comment your queries or suggestions.

References

Harmony OS: https://www.harmonyos.com/en/develop/?ha_source=hms1

Lightweight Preference database:

https://developer.harmonyos.com/en/docs/documentation/doc-guides/database-preference-overview-0000000000030086?ha_source=hms1

--

--

No responses yet