【发布时间】:2019-06-23 10:47:29
【问题描述】:
我正在开发一个应用程序,有一个功能可以获取用户的所有联系人并将其上传到服务器。在这里,我使用 Firebase Firestore 作为我的后端。我可以获取所有联系人而不会重复。有人可以帮忙,我需要在哪个文档结构中保存用户的联系方式?请告诉我适当的格式,以免在 Firestore 中占用太多空间。
然后..
保存所有联系人后,我必须能够使用用户输入的号码获取联系人的姓名。这是我的要求。简而言之,我想知道如何为 Firestore 保存 2500 个联系人详细信息,以及如何借助用户在编辑文本中输入的联系人号码来读取单个联系人姓名。 (注:听说不能一次性保存2500个用户的联系方式,应该用批量之类的)
我尝试了下面的代码,但它只保存了前 800 个联系人。
`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();
}`
- 请告诉我应该如何在 Firestore(结构)中保存数据
- 请告诉我借助号码读取单个联系人姓名
【问题讨论】:
标签: java android firebase google-cloud-firestore android-contacts