Android developers come in, help solve------------android SMS batch speed optimization

Source: Internet
Author: User
Tags bulk insert

When doing the backup and restore function of SMS on Android, the idea of SMS recovery is very simple, the loop parsing file, each get a text message, call Smsprovider Insert method to insert the text message into the database, Smsprovider is the most basic class of SMS database operation, The Query,insert,delete and update methods of the parent class ContentProvider are overloaded, and in addition to the Insert method, there is a Bulkinsert method in the parent class ContentProvider, which is a bulk insert with the following code:

<span style= "FONT-SIZE:14PX;" >public int Bulkinsert (URI uri, contentvalues[] values) {        int numvalues = values.length;        for (int i = 0; i < numvalues; i++) {            Insert (URI, values[i]);        }        return numvalues;    } </span>

It can be seen that the function is simply a circular call to the Insert method, and insert is a virtual method, so the actual use will call to the Smsprovider insert method, and there is no speed advantage;

In the actual use of the process, the number of messages to restore a long time, the speed is a bit unbearable, decided to consider the speed of optimization;

Android database using ingenious Sqlite3, fortunately Sqlite3 is a support transaction, naturally think of the benefits of unified transaction to BULK INSERT, this is the first direction to try speed optimization,

<span style= "FONT-SIZE:14PX;" >db.begintransaction (); try {    //here database operation    db.settransactionsuccessful ();//Do not forget this phrase} catch (XXX) {   //xxxxx} finaly {    Db.endtransaction (); Commit}</span>

Poses other risks that are not allowed for SMS applications.

..........................................................................................................................................................................................

This was written very early, and the current attempt failed to write the reason:

The above also mentioned Bulkinsert this function, he does not use unified transaction method to bulk insert SMS, but loop call insert, and there is no speed on the advantages and considerations, I think Android developers, it is impossible not to know Sqlite3 unified Transaction method, Then why do they have to leave it??

The reason is that if the use of unified transactions brings another risk-the loss of the received text messages, the opening of the transaction is to lock the DB, that is, after the opening of a transaction, the other database write operations are not successful, so that if in a unified transaction operation of a lot of data, need to consume a certain amount of time, Then the text message arrives, it is unable to insert into the database, also will not receive this text message, and the text message restores this operation slow to compare, this more cannot be allowed; another risk is a deadlock, in the end because the Sqlite3 support for concurrency is not very good;

After my actual test, found that when the original data in the database is not a long time, the method of unified transaction batch insertion consumption is quite small, but with more and more data, will be more and more slowly ~ ~ ~ Better way to realize their own slowly groping it, the above attempt to learn it ....


Android developers come in, help solve------------android SMS batch speed optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.