我正試圖在Parse.com儀表板上存儲用戶聯繫人。我再次運行我的項目&再次存儲聯繫人。在分析中存儲聯繫人時的數據丟失
但每次我看到不同數量的聯繫人存儲,一些聯繫人(數據即朋友&電話號碼)已經丟失。
我使用這些代碼:
public class MainActivity extends Activity {
ParseObject testObject;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Parse.initialize(this, "2dSVesLx7lUKxhSb7B4bSmAOlIVAWONM8sIQTtZb", "CkikNpzXV2eR0QugHnZCQoQjbh6IDgHrESG0KIBS");
et = (EditText)findViewById(R.id.editText1);
et.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_DONE) {
// TODO do something
handled = true;
testObject = new ParseObject("India"+et.getText().toString());
readDistinctContacts("India"+et.getText().toString());
// readContacts();
}
return handled;
}
});
}
public void readDistinctContacts(String s) {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
ArrayList<ParseObject> contacts = new ArrayList<ParseObject>();
ArrayList<String> list = new ArrayList<String>();
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) == 1) {
System.out.println(name);
ParseObject testObject = new ParseObject(s);
testObject.put("names", name);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println(phone);
testObject.put("phonenumber", phone);
if(!list.contains(phone)) {
contacts.add(testObject);
}
list.add(phone);
}
pCur.close();
testObject.saveInBackground();
}
}
}
cur.close();
}
public void readContacts(){
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) ==1) {
System.out.println(name);
ParseObject testObject = new ParseObject("India"+et.getText().toString());
testObject.put("names", name);
// testObject.saveInBackground();
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println(phone);
testObject.put("phonenumber", phone);
}
pCur.close();
testObject.saveInBackground();
}
}
}
}
}
我已經使用這兩種方法就像在代碼readcontacts()
,readDistinctContacts()
,但兩者都讓我有同樣的問題。看看我的儀表板的快照,它顯示了不同的接觸次數,每次
任何解決方案來管理這個問題呢?
在此先感謝
UPDATE
感謝askoka,使用saveEventually()沒有數據丟失,但需要3-4分鐘存儲400個parseobjects(即接觸)由400推送請求! 。我希望存儲應該是快速和高效的,任何人都可以告訴我實現它的最佳方式嗎?
你可以添加'的System.out.println(cur.getCount());'你創建變量,看看是否總是導致相同數量的接觸之後? – Dehli 2014-09-29 14:44:15
是的相同數量的聯繫人712!我也在logcat中得到這個警告:遊標沒有事先關閉而終結() – 2014-09-29 15:41:41
是的同樣數量的聯繫人712!我也得到這個警告:光標沒有事先關閉完成() – 2014-09-29 15:42:23