2014-09-25 64 views
0

我正試圖在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(),但兩者都讓我有同樣的問題。看看我的儀表板的快照,它顯示了不同的接觸次數,每次 enter image description here

任何解決方案來管理這個問題呢?

在此先感謝

UPDATE

感謝askoka,使用saveEventually()沒有數據丟失,但需要3-4分鐘存儲400個parseobjects(即接觸)由400推送請求! 。我希望存儲應該是快速和高效的,任何人都可以告訴我實現它的最佳方式嗎?

+0

你可以添加'的System.out.println(cur.getCount());'你創建變量,看看是否總是導致相同數量的接觸之後? – Dehli 2014-09-29 14:44:15

+0

是的相同數量的聯繫人712!我也在logcat中得到這個警告:遊標沒有事先關閉而終結() – 2014-09-29 15:41:41

+0

是的同樣數量的聯繫人712!我也得到這個警告:光標沒有事先關閉完成() – 2014-09-29 15:42:23

回答

1

您的saveInBackground可能遇到問題,請嘗試傳入回調以查看是否有錯誤。

saveInBackground(new SaveCallback() { 
    public void done(ParseException e) { 
     if (e != null) Log.e("ParseAPITest", "saveInBackground failed: " + e.toString()); 
    } 
}); 

,並在自己的實現重試邏輯,或者你可以嘗試:

  • 更換saveInBackgroundwith saveEventually,如果在消息推送的錯誤,將重試。

  • 一批通過保存所有ParseObjects在列表and use saveAllInBackground preferably with callback您推送消息,這將try to batch your push requests as discussed in saveAll

  • 如果您的應用程序的使用情況允許的話,您可以創建只是一個ParseObject和所有的聯繫人添加到這個單一ParseObject和調用saveEventually - 這應該只需要一個推送請求消息來解析服務器,所以延遲會減少。

    • 在你readDistinctContacts功能,您已經有了contacts ArrayList,在其中保存每個聯繫人爲ParseObject。您只需將該列表保存到一個根ParseObject並在其上調用saveEventually() - 這將導致對分析服務器的一個請求。

      • 就在while循環之前添加ParseObject root = new ParseObject(s);和while循環與ParseObject testObject = new ParseObject("Contact" + counter);取代ParseObject testObject = new ParseObject(s);。擺脫testObject.saveInBackground();

        最後在while循環後添加root.addAll(root.getClassName(), contacts);root.saveEventually();。這應該一次性保存所有聯繫人(請求),並且saveEventually將處理所需的任何重試。

+0

我得到一個問題,我在聊天中討論,請訪問http://chat.stackoverflow.com/rooms/62389/discussion-between-ashoke-and -vwvwvwvwvwvwvwvwvw – 2014-10-03 17:59:19

+0

發現問題,我們不能在類名中使用空格。刪除'ParseObject testObject = new ParseObject(「Contact」+ counter ++)中的「Contact」字符串中的空格;' – ashoke 2014-10-03 18:19:42

+0

仍然會出現此問題https://www.parse.com/questions/eparsecommandcache-saveeventually-thread-had-an - 錯誤 – 2014-10-04 03:25:49