directory

SharedPreferences 2, SQLite database storage 3, ContentProvider 4, File storage 5, network storage

First, the SharedPreferences

1.1 Application Scenarios

Suitable for a relatively small set of saved key-value pairs, the SharedPreferences object points to files that contain key-value pairs and provides a simple way to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared.

The essence is key-value data stored based on XML files, usually used to store some simple configuration information

1.2 Precautions

1.2.1 write operation

1. Do not store large Values. These keys and values will always exist in memory, occupying a large amount of memory

When obtaining the value from SP for the first time, it may block the main thread (when creating SharedPreferences for the first time, the file needs to be loaded. At this time, if the data needs to be read, we need to await the read part. If the waiting time in the main thread is too long, it will lead to ANR), so that the interface gets stuck and frames drop. SharedPreferences can be initialized prior to starting optimization

2, do not store complex data SharedPreferences through XML storage parsing, JSON or HTML format stored in SP, need to escape, this will bring a lot of special symbols, SP parsing encounter this special symbol will be special processing, Raises additional string concatenation and function call overhead. If the data is large and complex, it can lead to frequent GC in severe cases.

3, do not use sp in multi-process mode, because SharedPreferences cannot guarantee singleton in multi-process mode, if multiple processes operation may cause data problems.

The ContentProvider scheme supports cross-process access and uses ContentProvider to access all SP operations.

1.2.2 read operation

Submit Edit to create an Editor object (preferably cached). Each time apply is applied, threads are created for memory and disk synchronization

2. Recommend apply instead of commit

1.2.3 Poor lock performance

The SP GET operation locks the SharedPreferences object and excludes other operations. SP put, getEditor, and commitToMemory lock the SharedPreferences object, PUT locks the Editor object, and writing to disk locks a write lock.

1.3 What is the difference between Apply and commit

Apply commits data atoms to memory and then asynchronously commits them to hard disk. Commit commits data atoms asynchronously to hard disk. They wait for the commit being processed to be saved to disk before operating, thus reducing efficiency. However, apply is only atomic commits to content, and subsequent functions that call Apply will directly overwrite the previous memory data, thus improving efficiency to a certain extent. 3. The Apply method does not prompt for any failures. Since sharedPreference is a single instance in a process, concurrency conflicts generally do not occur. If you do not care about the result of the submission, it is recommended to use Apply. Of course, if you need to ensure the successful submission and subsequent operations, you still need to use COMMIT.

SQLite database storage

A lightweight embedded database engine, which is used to store a large number of complex relational data

2.1 Do you know about transactions in SQLite? How is it done

When SQLite performs CRDU operations, the transaction is enabled by default, and the SQL statement is translated into the corresponding SQLiteStatement and the corresponding CRUD method is called. At this time, the whole operation is still carried out on the temporary file rollback Journal, and the DB database will be updated only when the operation is successfully completed. Otherwise it will be rolled back

2.2 Is there a good way to use SQLite to do batch operations?

Start a transaction with the beginTransaction method of SQLiteDatabase, convert the batch operation SQL statement to SQLiteStatement and execute the batch operation, and then endTransaction()

2.3 How do I Delete certain fields in an SQLite table

SQLite database only allows you to add fields, but not modify or delete table fields. You can only create a new table to retain the original fields and delete the original table

2.4 What are the optimizations when using SQLite?

Close Cursor in time to avoid memory leaks. 3. Asynchronize time-consuming operations: Database operations are time-consuming local I/O operations, and it is recommended to place them in asynchronous threads. ContentValues internally uses HashMap to store key-value data. The initial capacity of ContentValues is 8, which will be doubled when expanded. Therefore, it is recommended to estimate the content filled in ContentValues, set a reasonable initial capacity, and reduce unnecessary internal capacity expansion operations. 5. Use indexes to speed up search: Indexes are recommended for large query operations and high service requirements

Third, ContentProvider

About ContentProvider basic knowledge, portal Yang said: ContentProvider knowledge summary

One of the four components, used for data storage and sharing, not only allows different applications to share data, but also can choose which part of the data to share, to ensure that the privacy data in the program does not have the risk of leakage;

4. File storage

Files are written and read in the same way as I/O programs in Java

More popular is Tencent’s open source MMKV

4.1 MMKV

4.4.1 MMKV principle

1. Use MMAP to map memory and files together and manipulate memory as if it were a file. 2, the application crash does not affect data loss 3, the efficiency is higher than SP

4.1.1.1 Protobuf agreement

Protobuf is a Google open source serialization framework, similar to XML and JSON. The biggest feature is based on binary, which is much shorter than the traditional XML representation of the same piece of content.

Protobuf is a binary storage format. The first digit represents the total length of the key and value, followed by the key length ->key and value length ->value

MMKV stores data based on the Protobuf protocol. The storage mode is incremental update, that is, all data does not need to be written to a file every time data is modified.

4.1.2 MMKV defects

1. When writing data, you need to copy it twice. If the data is in user -> kernel -> disk mode, too much data will cause performance loss

Five, network storage

Data is stored in a remote server, and the data of user operations can be synchronized to the server.

reference

2. Android: 3, SharedPreferences use performance optimization 4, SharePreferences use summary, advantages and disadvantages (including upgrade, in-depth understanding, 6. The principle and Realization of MMKV (2) 7. The principle and Realization of MMKV (3)