Modificado o Storage para ser um singleton
This commit is contained in:
parent
b37294e5a8
commit
296727ead2
@@ -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))
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user