Android implements similar wechat caching and instant update of friends’ avatars

introduction

When using wechat, we will find that when entering the wechat friends list for the first time, the friends' Avatar will be loaded, but when entering again, there is no need to reload, and other pages do not need to be reloaded, which shows that the friends' Avatar of wechat is cached locally, and then the friends will update it in time after modifying the avatar. How is this function realized, Let's analyze it

analysis

Implementation of avatar cache

The avatar is a network picture with a large amount of data. If the avatar is stored in the form of bitmap with our commonly used SharedPreferences, it is bound to cause oom. This method is not feasible. We can only store the address of the picture, but if we only store the address, we need to convert it into a picture or reload it through the network request, which can not meet our requirements, Therefore, we need to open up a separate space in the disk to store the avatar in the form of bitmap. How to achieve it? In fact, there are many open-source third-party frameworks for network image caching, which are more reliable and easy to use, such as xutils, glide, volley, universal image loader, Picasso, fresco and so on.

Let's take the commonly used xutils as an example. First, we instantiate bitmaputils. The disk cache path, disk cache space, memory cache space and memory cache percentage can be customized or configured by default. The code is as follows:

Generally, we only need to use the default configuration, that is

Then cache and display the picture

From this code, we can see that when we want to load a picture, we will find out whether there is a corresponding bitmap cache picture according to the picture address. If there is, we will directly reference the cache. If not, we will load and cache the picture. Therefore, we only need to implement the above methods for picture caching, and as long as we set the same cache path, After a page is cached, other pages with the same picture can also be called. So after caching, how do friends update avatars in real time?

How to update avatars immediately after caching

According to the data consulted, it can be summarized into the following implementation methods:

1. When the server returns the user array, add an additional flag such as the last modification time or several modifications of the field avatar, and compare it with the cache to see if there is any change

2. Use the checksum of the picture. If the number is changed, it will be updated automatically

3. Using socket monitoring, when a friend's Avatar is updated, it will first tell the server that the server will push the change notification to all friends. After receiving the notification, the friend monitoring will automatically update. The first method and the second method are essentially the same. By requesting the server's data to be compared with the local cache, it is processed by the client, In the third way, if you change your avatar once, you need the server to remind all your friends. Will the server be under great pressure

If you study wechat carefully, you will find that if you stay on a page after modifying your friend's Avatar, the avatar will not change if you have entered it before and haven't been destroyed. You need to open a new page or re-enter wechat to update the avatar. From this, we can see that wechat is not the third way, Instead, the implementation principles of the first two methods are adopted. The avatar will be updated only when an activity or fragment is created, the interface is called and the server data is read

summary

Through the above analysis, we have basically clarified our thinking. To realize the caching and updating of wechat like avatars, first open up a space on the disk for reading and writing the bitmap of avatars, and then read the server data and compare it with the local cache when creating a page, and update it if there is any change

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>