Question

I have following class to get contacts and store in my sqlite database. This takes around 1:45 minutes to store 635 contacts.

I want to improve performance of this class. Please can anyone provide suggestions to improve this class ??

   // Inner class for Retrieving all contacts form phone
   private class ContactRetrievalService extends AsyncTask<Void, Void, Boolean>{

    ProgressDialog progressDialog;
    String projection[] = { Data.CONTACT_ID,
                            Data.RAW_CONTACT_ID,
                            Data.DISPLAY_NAME,
                            Data.HAS_PHONE_NUMBER,
                            Data.STARRED,
                            Email.DATA,
                            Phone.NUMBER,
                            StructuredPostal.STREET,
                            StructuredPostal.POSTCODE,
                            Organization.COMPANY,
                            Organization.TITLE};        

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        progressDialog = ProgressDialog.show(ActivityContactRetrieval.this, "Retrieving Contacts", "Please wait...");

        /* if (progressDialog == null) {
               progressDialog = createProgressDialog(ActivityContactRetrieval.this);
               progressDialog.show();
               } else {
               progressDialog.show();
               }*/
    }

@Override
protected Boolean doInBackground(Void... params) {
    // TODO Auto-generated method stub
    datasource = new ContactDataSource(ActivityContactRetrieval.this);
    datasource.open();



    Cursor c = getContentResolver().query(
                Data.CONTENT_URI,
                projection,
                "(" + Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?) ", 
                new String[]{Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE },
                Data.CONTACT_ID);

    while (c.moveToNext()) {        

        for(int i = 0; i<c.getColumnCount();i++){
            Log.e("STORED " ,c.getColumnName(i) +":"+ c.getString(i));
        }
        Log.e("STORED END:", "-----------------------------------------------------------------------");            

        long id = c.getLong(c.getColumnIndex(Data.CONTACT_ID));               
        final String contact_id = String.valueOf(id);

        String nickName = c.getString(c.getColumnIndex(Data.DISPLAY_NAME));            
        String raw_contact_id = c.getString(c.getColumnIndex(Data.RAW_CONTACT_ID));         
        int    starred = c.getInt(c.getColumnIndex(Data.STARRED));            
        String firstName="",lastName="";

        if(nickName.contains(" ")){
            firstName = nickName.substring(0, nickName.indexOf(" "));
            lastName = nickName.substring(nickName.indexOf(" "),nickName.length());
        } 

       int has_phone = c.getInt(c.getColumnIndex(Data.HAS_PHONE_NUMBER));

        //To store conatct detail in Contact table
        if( has_phone >= 0 && !isAlreadyExists(MySQLiteHelper.TABLE_NAME_CONTACT, MySQLiteHelper.COLUMN_CNT_CONTACT_ID, contact_id)){

              Contact contactData = new Contact();                        
              contactData.setContact_id(contact_id);

              try{
              if( firstName!=null || !firstName.equalsIgnoreCase("") ){//|| !firstName.equalsIgnoreCase(number)
                  contactData.setFirstName(firstName);
              }
              if( lastName!=null || !lastName.equalsIgnoreCase("")  ){//|| lastName.equalsIgnoreCase(number)
                  contactData.setLastName(lastName);
              }
              }catch(Exception e){
                  Log.e(CLASS_NAME,e.toString());
              }    

              contactData.setNickName(nickName);
              contactData.setIsFavorite(starred);

              HashMap<String, Contact> map =  new  HashMap<String, Contact>();
              map.put("data", contactData);

              long newId = datasource.createContact(map);                     
              Log.e("insert Contact id :", String.valueOf(newId));  
        }       


        // Create Inner Thread Class
        Thread phoneThread = new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub

                // To store phone numbers in Phone table
                Cursor phone = getContentResolver().query(Phone.CONTENT_URI,new String[]{Phone._ID,Phone.NUMBER,Phone.CONTACT_ID}, Phone.CONTACT_ID + " =?", new String[]{contact_id}, null);
                String number="";
                while(phone.moveToNext()){

                    Log.e("PHONE NUMBER :",phone.getString(phone.getColumnIndex(Phone.NUMBER)));
                     number = phone.getString(phone.getColumnIndex(Phone.NUMBER));

                    if(!isAlreadyExists(MySQLiteHelper.TABLE_NAME_PHONE, MySQLiteHelper.COLUMN_PHN_NUMBER, number)){

                        try{                    
                            if( number!=null || !number.equalsIgnoreCase("") ){

                                Phones phoneData = new Phones();                      
                                phoneData.setContact_id(contact_id);
                                phoneData.setNumber(number);

                                HashMap<String,Phones> map =  new  HashMap<String, Phones>();
                                map.put("data", phoneData);

                                long newId = datasource.createPhone(map);                     
                                Log.e("insert phone id :", String.valueOf(newId));
                            }                         
                          }catch(Exception e){
                              Log.e(CLASS_NAME,e.toString());
                          }                     
                    }// else end of phone
                }
                phone.close();
                Log.e("THREAD:","Started");
            }               
        });
        phoneThread.start();


        // Create Inner Thread Class
        Thread emailThread = new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub

                 //To store email id in Email table
                Cursor email = getContentResolver().query(Email.CONTENT_URI,new String[]{Email._ID,Email.DATA,Email.CONTACT_ID}, Email.CONTACT_ID + " =?", new String[]{contact_id}, null);
                String emailId="";
                while(email.moveToNext()){

                    emailId  = email.getString(email.getColumnIndex(Email.DATA));
                    if(isAlreadyExists(MySQLiteHelper.TABLE_NAME_EMAIL, MySQLiteHelper.COLUMN_EML_EMAIL_ID, emailId)){
                        Log.e("DUPLICATE:","Is here");
                    }else{                                                          
                         try{
                             if( emailId!=null || !emailId.equalsIgnoreCase("") ){//|| !emailId.equalsIgnoreCase(number)

                                 Emails emailData = new Emails();                         
                                 emailData.setContact_id(contact_id);
                                 emailData.setEmailId(emailId);
                                 HashMap<String,Emails> map =  new  HashMap<String, Emails>();
                                 map.put("data", emailData);

                                 long newId = datasource.createEmail(map);                    
                                 Log.e("insert email id :", String.valueOf(newId));
                                }                         
                          }catch(Exception e){
                              Log.e(CLASS_NAME,e.toString());
                          }                 
                    }// else end of email
                }
                email.close();
            }
            });
            emailThread.start();    


            // Create Inner Thread Class
            Thread addressThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub

                      //To store address in address table
                    Cursor address = getContentResolver().query(StructuredPostal.CONTENT_URI,new String[]{StructuredPostal._ID,StructuredPostal.STREET,StructuredPostal.CITY,StructuredPostal.POSTCODE}, StructuredPostal.CONTACT_ID + " =?", new String[]{contact_id}, null);
                    String street="",city="",zipCode="";
                    while(address.moveToNext()){

                        street = address.getString(address.getColumnIndex(StructuredPostal.STREET));
                        city = address.getString(address.getColumnIndex(StructuredPostal.CITY));
                        zipCode  = address.getString(address.getColumnIndex(StructuredPostal.POSTCODE));

                        if(isAlreadyExists(MySQLiteHelper.TABLE_NAME_ADDRESS, MySQLiteHelper.COLUMN_ADR_CONTACT_ID, contact_id)){
                            Log.e("DUPLICATE:","Is here");
                        }else{                                                          
                             try{
                                 if( street!=null || !street.equalsIgnoreCase("")){// || !emailId.equalsIgnoreCase(number)

                                     Address addressData = new Address();                         
                                     addressData.setContact_id(contact_id);
                                     addressData.setCity(city);
                                     addressData.setStreet(street);
                                     addressData.setZipCode(zipCode);

                                     HashMap<String,Address> map =  new  HashMap<String, Address>();
                                     map.put("data", addressData);

                                     long newId = datasource.createAddress(map);                      
                                     Log.e("insert email id :", String.valueOf(newId));
                                    }                         
                              }catch(Exception e){
                                  Log.e(CLASS_NAME,e.toString());
                              }                 
                        }// else end of email
                    }
                    address.close();            
                    }
                });
               addressThread.start();   

            }       
    datasource.close();                  

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = preferences.edit();
    editor.putBoolean("Contact_saved", true);
    editor.commit();

    return true;

    }// end of doInBackground method

    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Boolean result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if (progressDialog!=null) {
            progressDialog.dismiss();
            // Go to home screen
            Intent i = new Intent(ActivityContactRetrieval.this, ActivityHomeScreen.class);
            startActivity(i);
            // And finish this activity
            finish();
        }
    }   
}

 /*// Custom ProgressDialog
public static ProgressDialog createProgressDialog(Context mContext) {
    ProgressDialog dialog = new ProgressDialog(mContext);
    try {
            dialog.show();
    } catch (BadTokenException e) {

    }
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.progress_dialog);
    // dialog.setMessage(Message);
    return dialog;
}*/

public boolean isAlreadyExists(String table,String column,String key) throws SQLException {
    int count = -1;
    Cursor c = null; 
    MySQLiteHelper dbHelper = new MySQLiteHelper(getApplicationContext());
    SQLiteDatabase database  = dbHelper.getWritableDatabase();
    database = dbHelper.getWritableDatabase();
    try {
       String query = "SELECT COUNT(*) FROM " + table + " WHERE " + column + " = ?" ;
       c = database.rawQuery(query, new String[] {key});
       if (c.moveToFirst()) {
          count = c.getInt(0);
       }
       return count > 0;
    }
    finally {
       if (c != null) {
          c.close();
          database.close();
       }
    }
Was it helpful?

Solution

In you method isAlreadyExists make the following changes:

  1. do not get a writable database, readabale would be better. Better still can you cache this value in a variable and avoid opening and closing it every time
  2. rather than select count use a select * from table limit 1

OTHER TIPS

Some issues I found:

1.-Forget about all the threads. All the work is already being done in the AsyncTask. Having all those threads add a HUGE amount of overhead, specially because you could have literally dozens of threads running at the same time.

2.-I notice that you have code like this:

  Address addressData = new Address();                         
  addressData.setContact_id(contact_id);
  addressData.setCity(city);
  addressData.setStreet(street);
  addressData.setZipCode(zipCode);
  HashMap<String,Address> map =  new  HashMap<String, Address>();
  map.put("data", addressData);
  long newId = datasource.createAddress(map);  

I guess that last line creates a record in a database. It would be easier if you create a query directly, because creating many objects and letting them be collected by the GC can cause overhead too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top