Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to operate local storage synchronously or asynchronously in Mini Program

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Mini Program how to synchronously or asynchronously operate local storage, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

This article takes you to understand the synchronous asynchronous storage in WeChat Mini Programs, and introduces the methods of synchronous operation of local storage and asynchronous operation of local storage. I hope it will be helpful to you!

one。 Synchronous operation local storage

Unless necessary, try to use synchronous methods, especially beginners, and it is recommended to use synchronous methods, unless synchronous methods do not solve the problem. Consider using asynchronous methods. [recommended for related study: Mini Program Development tutorial]

Wx.setStorageSync synchronization memory: wx.setStorageSync ('key',' value')

The effect can be seen in WeChat Mini Programs debugger.

Wx.getStorageSync synchronous acquisition: wx.getStorageSync ('key') console.log (wx.getStorageSync (' key')) / / valuewx.getStorageInfoSync () Information in the current storage const res = wx.getStorageInfoSync () console.log (res.keys) / / ["logs", "key"] / / res.keys the amount of space currently occupied by all keyconsole.log (res.currentSize) / / 1//res.currentSize in the current storage Space size restricted by unit KBconsole.log (res.limitSize) / / 10240//res.limitSize Unit KBwx.removeStorageSync removes one synchronously: wx.removeStorageSync ('key')

After removal, the storage called key will disappear.

Wx.clearStorageSync synchronously clears all: wx.clearStorageSync ()

The use of clearStorageSync, together with the previous logs, will be cleared.

two。 Asynchronous operation local storage 1.wx.setStorage asynchronous storage values

Stores the data in the key specified in the local cache. Will overwrite the corresponding content of the original key. Data is always available unless the user actively deletes it or is cleaned up by the system due to storage space. The maximum length of big data allowed for storage in a single key is 1MB, and the upper limit for all data storage is 10MB.

Wx.setStorage ({key: "key2", data: "value2"})

When we store the value, we can see the effect in WeChat Mini Programs's debug bar. Except for the different operations of access and execution, the result is the same, all save, take the value, and the result is the same. it's just that synchronization is executed sequentially, while async won't make the interface stagnate, but this can almost be ignored, so it's recommended that you use synchronization if you don't have to.

2.wx.removeStorage () removes the specified value

Removes the specified key from the local cache.

Wx.removeStorage ({key: 'key', success (res) {console.log (res)}}) 3.wx.getStorage (); get the value

Gets the contents of the specified key asynchronously from the local cache.

Wx.getStorage ({key: 'key', success (res) {console.log (res.data)}}) 4.wx.getStorageInfo gets the information in the current storage wx.getStorageInfo ({success (res) {console.log (res.keys) / / ["logs", "key"] / / all key console.log (res.currentSize) / / 1 / / currently occupied space in the current storage Limited space size per unit KB console.log (res.limitSize) / / 10240 / / Unit KB}}) 5.wx.clearStorage () Clear all the keywx.clearStorage () after reading the above, do you know how to operate local storage synchronously or asynchronously in Mini Program? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report