使用查询从Firestore有效地检索联系人数据

时间:2019-07-10 09:02:01

标签: android firebase google-cloud-firestore android-contacts

我的要求是,当用户Mr.ABC安装该应用程序时,它将获取该用户的所有联系人并保存到Firebase Firestore。当另一个用户Mr.XYZ在其设备中安装该应用程序时,它将获取XYZ的所有联系人并保存到Firebase。同样,安装该应用程序的任何人都将获取联系人并保存到Firebase Firestore。现在我的读取操作应该是,ABC先生将在EditText和firebase中键入XYZ号码,我必须查看XYZ联系人详细信息并找出ABC的号码,并获取在联系人中保存XYZ的名称。因此,当ABC输入XYZ号码时,ABC可以找出XYZ用哪个名称保存了ABC的号码。请帮助我将联系人保存在firebase中的结构以及如何读取数据。我想知道有效的方法。由于我不太擅长数据库,因此为我提供准确的代码将大有帮助。请帮助。enter image description here

我尝试了以下代码,但是只有800个联系人存储到Firestore中,而不是2500个,而且我不确定我使用的数据库结构是对还是错。我对DB的知识为零。

    private void doUploadContactstoCloud() {
        dialog.setCancelable(false);
        dialog.setMessage("Please wait we are configuring your request ...");
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.show();
        listContacts = new ContactPresenter(this).fetchAllContacts();
        int j = listContacts.size();

        Log.d("sizejhfg",j+"");

        double size = listContacts.size();
        double ind = 0;
        Map<String, Object> contact = new HashMap<>();
        // Get a new write batch
        WriteBatch batch = db.batch();
        for (Contact con : listContacts) {
            for (int i = 0; i < con.numbers.size(); i++) {
                String number = formatnum(con.numbers.get(i).number);
                if (number != null) {
                    if (!number.equals("0") && number.length() < 5) {

                        Map<String, Object> contactnumber = new HashMap<>();
                        contactnumber.put("name", con.name);
                        contactnumber.put("number", number);
                        contact.put(number,contactnumber);
                        DocumentReference item = db.collection(tonumber).document(number);
                        batch.set(item,contactnumber);
                        ind++;
                        if(ind==500){
                            doCommit(batch);
                            batch=db.batch();
                            ind=0;
                        }
                    }
                }
            }
            //dialog.setProgress((int) (ind / size) * 100);
        }
        if(ind>0){
            doCommit(batch);
        }
        prefManager.setisContactUpload(true);
        doDismissDialog();
    }` 

1 个答案:

答案 0 :(得分:1)

以下是您问题的可能答案-

首先是Firestore数据库结构。我现在将其称为db结构。这是一个可能适合您的模板-

c- Contacts
   d- +1 9999999999 //ABC's Phone No.
      f- +1 0000000000 : XYZ's Number // XYZ's Saved Name in ABC's Contacts and his phone no.
      f- +1 1111111111 : LMN's Number // LMN's Saved Name in ABC's Contacts and his phone no.
   d- +1 0000000000 //XYZ's Phone No.
      f- +1 9999999999 : ABC's Number //ABC's Saved Name in XYZ's Contacts and his phone no.
      f- +1 1111111111 : LMN's Number // LMN's Saved Name in XYZ's Contacts and his phone no.

第二,这是我认为适用于添加和查询数据库的代码-

Map<String, Object> contacts = new HashMap<>();
contacts.put("+1 0000000000", "XYZ's Number");
contacts.put("+1 1111111111", "LMN's Number");

db.collection("contacts").document("+1 9999999999")
        .set(contacts)
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Log.d(TAG, "DocumentSnapshot successfully written!");
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.w(TAG, "Error writing document", e);
            }
        });

编辑此部分以获得更有效的查询-

db.collections("contacts").document("+1 9999999999").get(EditTextEnteredNumber)
        .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                for (QueryDocumentSnapshot snap : queryDocumentSnapshots) {
                    Log.d(TAG, snap.getId() + " => " + snap.getData());
                    TextViewContactName.setText(snap.getData());
                }
            }
        });

希望这会有所帮助!

来源