当前位置:网站首页>The difference between setstoragesync and setstorage
The difference between setstoragesync and setstorage
2022-06-26 07:48:00 【GHUIJS】
I have always liked to use native local storage in browsers , I used it a lot before . Developed a few days ago uniapp, Use the browser's native local storage , No problem in the browser , But packaged into app It can't be used . Later on uniapp Official local storage , Can be used after packaging , It has to be said that the official is
, But the first time I used it , This synchronization and asynchronism makes me a little confused . But the existence of synchronous and asynchronous , There must be a reason .
Let's start with the official definition :
uni.setStorage(OBJECT)
Store the data in the local cache specified key in , Will cover up the original key Corresponding content , This is an asynchronous interface .
uni.setStorage({
key: 'storage_key',
data: 'hello',
success: function () {
console.log('success');
}
});uni.setStorageSync(KEY,DATA)
take data Store... Specified in the local cache key in , Will cover up the original key Corresponding content , This is a synchronous interface .
uni.setStorageSync('storage_key', 'hello');Just look at the way it is written , The synchronous interface is easy to use , Follow js Native storage is no different , Save key value pairs . The asynchronous interface can receive callback after accessing data .
Next , Let's try it .
uni.setStorage({
key:'ghui',
data:' I'm dregs and dregs ',
success() {
uni.showToast({
title:' Saved successfully '
});
}
});
let ghui = uni.getStorage({
key:'ghui'
})
let GHUI = uni.getStorageSync('ghui');
console.log(ghui,GHUI);The console results are as follows :

Then I changed it :
uni.getStorage({
key:'ghui',
success(res) {
console.log(res);
}
})
That is to say , The asynchronous interface fetches the returned results from the cache , Can only be used in successful callbacks , The feature is that after getting the cached data, you don't have to wait for irrelevant logic to complete , Just execute the relevant logic directly , faster , But it's nested in layers 、 Not beautiful . Recall the first time the console printed promise, I seem to think of something .
uni.getStorage({
key:'ghui',
}).then(res => {
console.log(res);
})Sure enough , Yes. , But what you get is an array , There is a second item in the data :

In this case , You can avoid nesting layers . And the synchronization interface , Apart from being easy to write , He can assign a value to a variable , It's easy to use , And it is executed synchronously , There is no need to consider the following logic problems when the cache is not fetched , Used to be the same as the original , There will be no more writing , He that talks much errs much .
One sided understanding , No, I hope you guys can give me more advice .

边栏推荐
- 我想造SQL数据(存储结构)
- JS event loop mechanism
- [issue 22] sheen cloud platform one side & two sides
- Exploration and practice of incremental data Lake in station B
- PyTorch-12 GAN、WGAN
- ASP. Net and Net framework and C #
- Detailed explanation of the generate go file command of import in golang (absolute detail)
- CMDA 3634 image processing
- You can command Siri without making a sound! The Chinese team of Cornell University developed the silent language recognition necklace. Chinese and English are OK
- QT basics tutorial: qstring
猜你喜欢

Detailed explanation and code implementation of soft voting and hard voting mechanism in integrated learning

QT之一个UI里边多界面切换

What is the five levels of cultivation of MES management system

Household enterprises use WMS warehouse management system. What are the changes

Hand drawn style chart library chart Implementation principle of xkcd

Machine learning - Iris Flower classification

I want to create SQL data (storage structure)

Google Earth Engine(GEE) 01-中输入提示快捷键Ctrl+space无法使用的问题
![[UVM basics] TLM common data receiving and sending and data receiving examples](/img/4f/6c6e8b26124ba042f949291b944c3d.jpg)
[UVM basics] TLM common data receiving and sending and data receiving examples

Deeply analyze storage costs and find cost reduction solutions
随机推荐
Use middleware to record slow laravel requests
数据中心灾难恢复的重要参考指标:RTO和RPO
Sanic based services use celery to complete dynamic modification timing tasks
[UVM practice] Chapter 2: a simple UVM verification platform (5) build test cases
buuresevewp
Multisensor fusion sensing
Systemctl PHP configuration file
js模块化
How to quickly merge multiple PDF files?
Exit of shell internal value command
[recommend an entity class conversion tool mapstruct, which is powerful and easy to use]
Jemter 压力测试 -可视化工具支持-【安装篇】
The "big grievances" in the workplace are not only physically tired, but also mentally emptied
Household enterprises use WMS warehouse management system. What are the changes
Error reported by using two-dimensional array [[]] in thymeleaf: could not parse as expression
Data governance: from top project to data culture!
Install the local Google plug-in and watch the free video through the oil monkey script
Cloud native integration data warehouse heavy release
Machine learning - Iris Flower classification
Median segmentation (find rules) - Niuke