Hello everyone, today I'm going to share some information about state storage in the Cangjie development language. It mainly introduces two parts: AppStorage and PersistentStorage.
AppStorage
AppStorage is a global-level state storage for applications and can be used at any time anywhere within the application. First, let's introduce the storage methods of AppStorage. There are two ways: set and setOrCreate. The difference is that set can only set the value of a field that has already been created, which is equivalent to modifying the value. The function of the setOrCreate method is to automatically create a field when there is no field. So I recommend that everyone use the setOrCreate method.
let store1 = AppStorage.set('name', 'youlan');
let store2 = AppStorage.setOrCreate('name', 'youlan');
When taking values, it is recommended that you first determine whether the field exists in the local storage and use the has method:
let hasValue:Bool = AppStorage.has('name')
When it is determined that there is a value, the get method can be used to retrieve it. Note that the type should be indicated when retrieving:
if(hasValue){
let name = AppStorage.get<String>('name')
}
You can also add getOrThrow() after the value code, which means to take a value or throw an exception:
let name = AppStorage.get<String>('name').getOrThrow()
In addition to the basic store and read operations, you can also use link to bidirectionally bind the data in AppStorage. Let me demonstrate it to you:
let name1 = AppStorage.link<String>("name").getOrThrow()
let name2 = AppStorage.link<String>("name").getOrThrow()
name1.set('newname')
In the code, both name1 and name2 are bidirectionally bound to the name field in AppStorage. When the value of name1 is modified, name2 will also be synchronously modified to the new value newname.
Finally, let's introduce the methods for deleting data in Appstorage. You can use delete to remove the data of a certain field, or use the clear method to clear all the data:
AppStorage.delete('name')
AppStorage.clear()
PersistentStorage
PersistentStorage is based on AppStorage and retains data on the disk to achieve persistence. Currently, the data types supported by PersistentStorage include Int64, Float64, String, and Bool. In addition, it is not recommended that you use PersistentStorage to store large amounts of data. For data exceeding 2kb, it is suggested to use a database.
There are two storage methods for PersistentStorage. persistProps can store a single piece of data, and Persistprops can store multiple pieces of data simultaneously. For example, in the following code:
PersistentStorage.persistProp("name", "fugui")
PersistentStorage.persistProp("age", "20")
PersistentStorage.persistProps([("name", "fugui"),("age", "20")])
Since PersistentStorage persists data on the basis of AppStorage, the content stored in PersistentStorage can be read through AppStorage, and the method of obtaining values is the same as above.
Finally, let's introduce the deletion method of PersistentStorage:
PersistentStorage.deleteProp('name')
The above is the sharing content for today. Thank you for reading. #HarmonyOS Language ## Cangjie ## Shopping #
Top comments (0)