Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungRefreshed repository pull requests page generally available(22.09.2026 um 03:25 Uhr)
Sichere ProgrammierungThe Joy of Learning the Basics Again(22.09.2026 um 03:28 Uhr)
Sichere ProgrammierungZero-Code OpenTelemetry Tracing for Dagster(22.09.2026 um 03:39 Uhr)
Linux Tipps & Hardening`prime-all`(22.09.2026 um 02:28 Uhr)
IT Security Toolsopensoho v0.15.2(22.09.2026 um 03:33 Uhr)
IT Security NachrichtenUS Proposes AI Incident Alert System in Talks With China, Bessent Says(22.09.2026 um 04:01 Uhr)
Sichere ProgrammierungRefreshed repository pull requests page generally available(22.09.2026 um 03:25 Uhr)
Sichere ProgrammierungThe Joy of Learning the Basics Again(22.09.2026 um 03:28 Uhr)
Sichere ProgrammierungZero-Code OpenTelemetry Tracing for Dagster(22.09.2026 um 03:39 Uhr)
Linux Tipps & Hardening`prime-all`(22.09.2026 um 02:28 Uhr)
IT Security Toolsopensoho v0.15.2(22.09.2026 um 03:33 Uhr)
IT Security NachrichtenUS Proposes AI Incident Alert System in Talks With China, Bessent Says(22.09.2026 um 04:01 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

How to persistently store object-type data on a hard drive using PersistentStorage

Read the original article:How to persistently store object-type data on a hard drive using PersistentStorage How to persistently store object-type data on a hard drive using PersistentStorage Problem D…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Read the original article:How to persistently store object-type data on a hard drive using PersistentStorage






How to persistently store object-type data on a hard drive using PersistentStorage






Problem Description



PersistentStorage itself does not support the storage of object or array types. How can data be processed to meet the storage requirements and enable the storage of object and array types?






Background Knowledge




  • AppStorage: This is the application-wide UI state storage, which is bound to the application's process. It is created by the UI framework when the application starts, providing a central storage for the application's UI state properties.

  • PersistentStorage: This is the persistent storage of selected AppStorage properties, ensuring that these properties retain their values upon application restart, matching the values at the time of application closure.



Since PersistentStorage allows simple types such as number, string, boolean, and enum, it is considered to convert to string type for storage. JSON.stringify() can convert an object or value into a JSON string, so we can attempt to use this method to convert it into a string before storing it.






Troubleshooting Process





  1. Identify Issue: PersistentStorage only supports primitive types (number, string, boolean, enum). Objects/arrays cause errors.


  2. Serialize Data: Convert objects/arrays to JSON strings using JSON.stringify() before storage.


  3. Deserialize Data: On retrieval, parse strings back to objects/arrays with JSON.parse() (add error handling).


  4. Reactivity: Use @StorageLink for UI updates when stored data changes.






Analysis Conclusion



Solution: Serialize objects/arrays to JSON strings via JSON() for storage, then deserialize with JSON.parse() on retrieval.

Why It Works: JSON strings are primitive types supported by PersistentStorage.



Caveats:




  • Only works for JSON-serializable data (no methods/circular references).

  • Add try-catch for parsing errors and default values for empty storage.

  • Use @StorageLink to maintain UI reactivity.



Implementation:




// Storing
const data = [{ id: 1 }];
PersistentStorage.PersistProp('key', JSON.stringify(data));

// Retrieving
const storedData = JSON.parse(AppStorage.Get('key') || "[]");
Copy codeCopy code









Solution




  1. For classes without methods, convert the array data into a string using JSON.stringify() and then store it. When reading, simply parse it using JSON.parse() and use it directly. The example code is as follows:




   class Student {  
name: string
age: number

constructor(name: string, age: number) {
this.name = name
this.age = age
}
}

PersistentStorage.persistProp('studentArr', JSON.stringify([new Student('Tom', 16), new Student('Gina', 18)]));

@Entry
@Component
struct Index {
@State studentArr: Array<Student> = [];
@StorageLink('studentArr') @Watch('onStrChange') studentArrStr: string = '[]';

onStrChange() {
this.studentArr = JSON.parse(this.studentArrStr);
}

aboutToAppear(): void {
// The Watch event is not triggered during component initialization; the array is initialized through the aboutToAppear event.
this.studentArr = JSON.parse(this.studentArrStr);
}

build() {
Column({ space: 8 }) {
ForEach(this.studentArr, (item: Student, index: number) => {
Column() {
Text(`Student Name: ${item.name}`)
.width('100%')
Text(`Student Age: ${item.age}`)
.width('100%')
}
.borderRadius(12)
.width('100%')
.backgroundColor(Color.White)
.padding(16)
}, (item: Student) => JSON.stringify(item))
}
.width('100%')
.height('100%')
.backgroundColor('#f1f3f5')
.padding(12)
}
}







  1. For classes with method functions, it is necessary to first convert the string into a data array, then call the class constructor to create an object using each piece of data. This way, the created object will have a prototype chain and can call the corresponding methods. The reference code is as follows:




   class Student {  
name: string
age: number

constructor(name: string, age: number) {
this.name = name
this.age = age
}

selfIntroduction() {
console.log(`My name is ${this.name} and I'm ${this.age} years old.`);
}
}

PersistentStorage.persistProp('studentArr', JSON.stringify([new Student('Tom', 16), new Student('Gina', 18)]));

@Entry
@Component
struct Index {
@State studentArr: Array<Student> = [];
@StorageLink('studentArr') @Watch('onStrChange') studentArrStr: string = '[]';

onStrChange() {
const dataArr: Array<Student> = JSON.parse(this.studentArrStr);
this.studentArr = dataArr.map((item: Student) => new Student(item.name, item.age));
}

aboutToAppear(): void {
// The Watch event is not triggered during component initialization; the array is initialized through the aboutToAppear event.
const dataArr: Array<Student> = JSON.parse(this.studentArrStr);
this.studentArr = dataArr.map((item: Student) => new Student(item.name, item.age));
}

build() {
Column({ space: 8 }) {
ForEach(this.studentArr, (item: Student, index: number) => {
Column() {
// UI interface reference the code from the previous solution
Button('Get Self-introduction')
.onClick(() => {
item.selfIntroduction();
})
}
// Style reference the code from the previous solution
}, (item: Student) => JSON.stringify(item))
}
// Style reference the code from the previous solution
}
}









Verification Result



kbs--93e146b7ae4b49d2a6536e1261681db0-2c5b3.pngimage.png






Summary



To store an array of objects as data to the hard drive using PersistentStorage, you need to convert the objects into strings using JSON.stringify() before storing them. When using the data, handle it differently based on whether the class contains methods. For data classes without methods, simply parse them using JSON.parse() and then use them. For classes with methods, you need to call the class's constructor to create an object before the object can invoke the corresponding methods; otherwise, there is a high likelihood of application crashes.






Written by Emrecan Karakas

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How to persistently store object-type data on a hard drive using PersistentStorage

Thematisch verwandte Begriffe: persistently, store, objecttype, data · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-49449 | Joplin is an open source note-taking and to-do application that organise…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick