gpt4 book ai didi

android - 如何提高存储数据的性能?

转载 作者:行者123 更新时间:2023-11-29 21:14:08 24 4
gpt4 key购买 nike

我有以下类(class)来获取联系人并将其存储在我的 sqlite 数据库中。这需要大约 1:45 分钟 来存储 635 联系人。

我想提高这个类的性能。请任何人提供改进此类的建议??

   // 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();
}
}

最佳答案

在您的方法 isAlreadyExists 中进行以下更改:

  1. 不要得到一个可写的数据库,可读的会更好。更好的是你可以把这个值缓存在一个变量中,避免每次都打开和关闭它
  2. 而不是select count使用select * from table limit 1

关于android - 如何提高存储数据的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21898931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com