This article is about caching and serialization of Android data. For complex data, database storage is needed, but relatively simple data that does not need complex queries and other operations are usually accessed by caching. There are three main cache methods: Sharedpreference, serialization and local file. #### Basic ways of Android data caching:
- 1, Sharedpreference
- 2. Serialization
- Serializable
- Parcelable
- 3. Local files
- Ordinary string
- Json string
#####2.1 Simple data access to access simple data, with Google official Sharedpreference is the most appropriate choice, SP supports the storage of basic types of data. However, it should be noted that you should not use Sharedpreference to store complex data. For details, please refer to this article “Talk about some Misunderstandings of Sharedpreference”.
#####2.2 Complex data access ######2.2.1 Serializable
######2.2.3 Writing to a local file
- Write in common string mode
- Normal mode write
A common string that does not require special processing and is directly written to a local file.
- Base64 encoding
For some special data, Base64 encoding can be used to write local files in order to prevent format changes. For Base64 encoding, you can refer to the principle and application of Base64 encoding in this article.
- Object into a Json string to access For complex object, in the hope that more easily recovered from a local file back and don’t want to maintain complex version control logic, the object into a Json string, deposited in the local file is a good choice, but also should pay attention to this way, the cached data object is best not to have a field rename, add fields, The default value of the new field will be the same as the default value of the new field that was deserialized from the old version of the cache. The content attribute is changed to List, in which case deserialization using automatic parsing tools like Gson will fail with a type mismatch exception.
#####2.3 Intent to pass complex data ######2.3.1 Serializable interface is relatively simple, the class and its internal members can implement Serializable interface, does not require additional code, but is slow in performance and speed. See this article: Parcelable vs Serializable. ######2.3.2 Parcelable mode is more than 10 times faster than Serializable, but the implementation is more complex, about the use of Parcelable please see this article :Android serialization complete parsing (ii)-Parcelable
But this is just the mainstream view of the circulation, this article Android serialization fully parsing (three)- to put things right, comparable to Dou ‘e Serializable author put forward a different view. Of course, this author has made some changes in the use of Serializable, the result is not comment, specific can be chosen according to their preferences.
For more information about simplifying Parcelable and reducing the cost of using it, see “Simplifying Parcel Operations” in this article, three ways to pass objects on Android, where the author lists a number of tools and third-party libraries for simplifying Parcelable.
######2.3.2 Converting an object to a Json String Converting an object to a Json string is performed as a string. After receiving the object, deserialization is performed. This may be the easiest way to do it, but it’s also the least efficient, according to the results of this article’s tests of three ways to pass objects around Android.
Reference articles: All articles mentioned in this article are clickable links to articles, and I thank the author. The following articles are also references for this article. 4.5.2 Transferring complex data in an Intent Parcelable vs Serializable
Click here for more recommended technical articles
Other interesting articles recommended: A brief history of Android Network request development and RxJava+Retrofit+OkHttp practice Android BaseAdapter minimalist encapsulation