'Faster/Fastest way to get new sms messages when starting app - Android
I have an sqlite database that contains all the sms messages of the user, however when you start the app I need to get all the sms's that I might not have if the user decides to use a different sms app inbetween.
I'm looking for the best way to sync my database with the base android content provider.
at the moment I do an Insert or Ignore for each sms like so :
insert or ignore into sms (smsID, smsCONID, smsMSG, smsNUM, smsREAD, smsTIMESTAMP, smsTYPE, smsSHORTMSG) values (?,?,?,?,?,?,?,?);
I load the inbox and sent messages in separate asynctasks in my splash screen activity this takes about 10s or so.
my doInBackground :
@Override
protected Void doInBackground(Void... arg0) {
int thread_id = 0;
int _id = 0;
int read;
String number;
boolean first = true;
String msg;
long timestamp;
/**
* RECUPERATION DES MESSAGES RECUS
*/
// PREPARE CURSOR
cursorInbox.moveToFirst();
while (cursorInbox.moveToNext()) {
if(first){
cursorInbox.moveToFirst();
first = false;
}
// RECUPERE THREAD_ID + ID_SMS + NUMERO DU CONTACT
thread_id = cursorInbox.getInt(cursorInbox.getColumnIndexOrThrow("thread_id"));
_id = cursorInbox.getInt(cursorInbox.getColumnIndexOrThrow("_id"));
number = cursorInbox.getString(cursorInbox.getColumnIndexOrThrow("address"));
msg = cursorInbox.getString(cursorInbox.getColumnIndexOrThrow("body"));
read = cursorInbox.getInt(cursorInbox.getColumnIndexOrThrow("read"));
timestamp = cursorInbox.getLong(cursorInbox.getColumnIndexOrThrow("date"));
// CREER LE SMS
dataManip.insert(_id, thread_id, msg, number, read, timestamp, "received",ContactsFactory.getMessageShort(msg));
i++;
publishProgress(i);
}
Log.d(TAG,"LoadActivity - Inbox Loaded");
return null;
}
Ideas for impovement?
Solution 1:[1]
if your database is indexed, than remove/disable the indexes: the inserts will be faster. Rebuild the indexes later in a new thread/task.
solution 2: don't write to database in first step just in memory( in a collection) later you will do the writings.
Solution 2:[2]
How about comparing the timestamps of each of the SMSes in the inbox with the latest timestamp in your database, and then only insert SMSes with a newer timestamp.
If you also start backwards, with the last SMS in the inbox, then you can break out of your loop as soon as you get to the first SMS older than your newest SMS.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | augustzf |
