2014-09-25 8 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에

감사에서

감사합니다,이 데이터 손실이 없었다하지만 400 parseobjects (예. 연락처) 400 푸시 요청에 의해 저장 3-4 분 정도 소요 saveEventually()를 사용! . 스토리지를 빠르고 효율적으로 만들고 싶습니다. 누구나 내가 할 수있는 최선의 방법을 말해 줄 수 있습니까?

+0

추가 할 수있는'에서 System.out.println (cur.getCount()); '당신이 변수를 생성하고 항상 접촉 같은 양의 결과 있는지 확인 후? – Dehli

+0

예 712 같은 수량의 연락처! 그리고 logcat에서 다음 경고 메시지가 나타납니다. close close()없이 완료된 커서 –

+0

예 712와 같은 양의 연락처! 그리고 나는 또한이 경고를 받는다 : 이전 close()없이 완료된 커서 –

답변

1

saveInBackground에 문제가있을 수 있으므로 콜백을 전달하여 오류가 있는지 확인하십시오.

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

과 자신에 재시도 논리를 구현하거나 시도 할 수 있습니다 :

  • 메시지 푸시에 오류가있을 경우 다시 시도합니다 saveInBackgroundwith saveEventually을 교체. 앱 사용 사례가 허용하는 경우 목록 and use saveAllInBackground preferably with callback의 모든 ParseObjects을 저장하여 푸시 메시지

  • 배치,이 try to batch your push requests as discussed in saveAll

  • , 당신은 단지 하나의 ParseObject을 만들고이 단일 ParseObject에 모든 연락처를 추가 할 수 있습니다 것입니다 saveEventually으로 전화하십시오 - 이것은 서버를 구문 분석하기 위해 푸시 요청 메시지 하나만을 받아야하므로 지연이 줄어 듭니다. 당신의 readDistinctContacts 기능에

    • 당신은 이미 당신이 ParseObject 각 연락처를 저장하는 contacts ArrayList을 보유하고 있습니다. 이 목록을 하나의 루트 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 –

+0

이 문제를 발견했습니다. 클래스 이름에 공백을 사용할 수 없습니다. ParseObject testObject = new ParseObject ("Contact"+ counter ++)의 "Contact"문자열에서 공간을 제거하십시오 : – ashoke

+0

여전히이 문제가 발생합니다 https://www.parse.com/questions/eparsecommandcache-saveeventually-thread-had-an -error –

관련 문제