我想将所有联系人保存和检索到 Firebase Firestore,需要结构来保存和检索联系人
i want to save and retrive all contacts to Firebase Firestore, need structure to save and retrive contacts
我正在开发一个应用程序,有一个功能可以获取用户的所有联系人并将其上传到服务器。在这里,我使用 Firebase Firestore 作为我的后端。我可以获得所有联系人而不会重复。有人可以帮忙,我需要在哪个文档结构中保存用户的联系方式?请告诉我适当的格式,这样在 Firestore 中就不会占用太多 space。
然后..
保存所有联系人后,我必须能够使用用户输入的号码获取联系人姓名。这是我的要求。简而言之,我想知道如何为 Firestore 保存 2500 个联系人详细信息,以及如何借助联系人号码读取单个联系人姓名,用户在 edittext 中键入什么。 (注意:我听说我们不能一次保存 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 中保存数据(结构)
- 请告诉我借助号码
读取单个联系人姓名
- Please tell how I should save data in Firestore (structure)
如果您使用身份验证,您的应用 use-case 可能的数据库模式可能是:
Firestore-root
|
--- users (collection)
|
--- uid (document)
|
--- // user properties
|
--- contacts (subcollection)
|
--- contactId (document)
|
--- phoneNumber: 123456789
|
--- // other contact properties
要获取用户拥有的所有联系人,只需在联系人引用上附加一个侦听器即可:
db.collection("users").document(uid).collection("contacts");
- please tell me to read single contact name with help of number
要获取号码为phone的单个联系人的详细信息,请使用以下查询:
db.collection("users").document(uid).collection("contacts").whereEqualTo("phoneNumber", 123456789);
根据有关 maximum number of documents that can be added in single batch 的官方文档:
Each transaction or batch of writes can write to a maximum of 500 documents.
因此,您必须将联系人分成 500 个文档的一部分,以便能够将所有这些联系人添加到 Firestore。
编辑:
if I assume one user is having 4000 contacts in his phone (just mobile number and name*4000 = 8000 data from a single user).
这是不正确的,如果您只有 2 个属性(在一个联系人文档中),您将只有 4000 次写入操作并且 而不是 8000 次,因为这两个属性是相同的文档。因此,您必须使用批量写入或使用 POJO class.
将它们一起写入
if the number of users become 5 million then it will be huge data that needs to be stored on in the Firestore.
这是正确的,但同时这意味着您将拥有一个非常成功的应用程序,并且您可以负担得起所有这些写入操作。 500万相信我,很多。
so I want a schema which must the best which should accommodate less space as possible
问题不在于 space,问题在于您执行的读写次数。 Firestore 中的一切都是关于读取和写入的数量。
I am expecting a very efficient way to save data. please help
在我看来,上面的模式可以帮助你解决问题。
considering this still your answer is the best solution?
考虑到您的问题和评论的要求,是的。
is it possible to ignore uid, contact id and can just have only these parameters
仅当您使用 Firebase realtime database 时,这是不同的产品。没有集合和文档,只有一个 JSON 数据库结构。
我正在开发一个应用程序,有一个功能可以获取用户的所有联系人并将其上传到服务器。在这里,我使用 Firebase Firestore 作为我的后端。我可以获得所有联系人而不会重复。有人可以帮忙,我需要在哪个文档结构中保存用户的联系方式?请告诉我适当的格式,这样在 Firestore 中就不会占用太多 space。
然后..
保存所有联系人后,我必须能够使用用户输入的号码获取联系人姓名。这是我的要求。简而言之,我想知道如何为 Firestore 保存 2500 个联系人详细信息,以及如何借助联系人号码读取单个联系人姓名,用户在 edittext 中键入什么。 (注意:我听说我们不能一次保存 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 中保存数据(结构)
- 请告诉我借助号码 读取单个联系人姓名
- Please tell how I should save data in Firestore (structure)
如果您使用身份验证,您的应用 use-case 可能的数据库模式可能是:
Firestore-root
|
--- users (collection)
|
--- uid (document)
|
--- // user properties
|
--- contacts (subcollection)
|
--- contactId (document)
|
--- phoneNumber: 123456789
|
--- // other contact properties
要获取用户拥有的所有联系人,只需在联系人引用上附加一个侦听器即可:
db.collection("users").document(uid).collection("contacts");
- please tell me to read single contact name with help of number
要获取号码为phone的单个联系人的详细信息,请使用以下查询:
db.collection("users").document(uid).collection("contacts").whereEqualTo("phoneNumber", 123456789);
根据有关 maximum number of documents that can be added in single batch 的官方文档:
Each transaction or batch of writes can write to a maximum of 500 documents.
因此,您必须将联系人分成 500 个文档的一部分,以便能够将所有这些联系人添加到 Firestore。
编辑:
if I assume one user is having 4000 contacts in his phone (just mobile number and name*4000 = 8000 data from a single user).
这是不正确的,如果您只有 2 个属性(在一个联系人文档中),您将只有 4000 次写入操作并且 而不是 8000 次,因为这两个属性是相同的文档。因此,您必须使用批量写入或使用 POJO class.
将它们一起写入if the number of users become 5 million then it will be huge data that needs to be stored on in the Firestore.
这是正确的,但同时这意味着您将拥有一个非常成功的应用程序,并且您可以负担得起所有这些写入操作。 500万相信我,很多。
so I want a schema which must the best which should accommodate less space as possible
问题不在于 space,问题在于您执行的读写次数。 Firestore 中的一切都是关于读取和写入的数量。
I am expecting a very efficient way to save data. please help
在我看来,上面的模式可以帮助你解决问题。
considering this still your answer is the best solution?
考虑到您的问题和评论的要求,是的。
is it possible to ignore uid, contact id and can just have only these parameters
仅当您使用 Firebase realtime database 时,这是不同的产品。没有集合和文档,只有一个 JSON 数据库结构。