Modificado o Storage para ser um singleton

This commit is contained in:
Gustavo Henrique Santos Souza de Miranda
2025-03-18 16:57:24 -03:00
parent b37294e5a8
commit 296727ead2
3 changed files with 73 additions and 9 deletions
+35
View File
@@ -0,0 +1,35 @@
import AsyncStorage from "@react-native-async-storage/async-storage"
export default class Storage{
static #instance;
constructor(){
if (Storage.#instance){
throw new Error("Use the The getInstanceMethod instead of new")
}
}
static getInstance(){
if (!Storage.#instance){
Storage.#instance = new Storage
}
return Storage.#instance
}
async listData(){
const data = await AsyncStorage.multiGet(await this.getKeys())
return data
}
async getKeys(){
const keys = await AsyncStorage.getAllKeys()
return keys;
}
async saveData(index,content){
await AsyncStorage.setItem(index.toString(),JSON.stringify(content))
}
}