检查集合是否存在 - Firestore
check collection existence - Firestore
示例:
在使用事件调度功能之前,我必须先与 Firestore 进行预约。
Example in the database:
- Companie1 (Document)
--- name
--- phone
--- Schedules (Collection)
-----Event 1
-----Event 2
我有一个执行新计划的函数。
根据例子。
我需要检查 Schedules 集合是否存在。
如果不存在我执行调度函数。如果我已经存在,我需要做另一个程序。
我已经用过这个模式了,但不对。
db.collection("Companies").document(IDCOMPANIE1)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
在继续注册之前,我需要帮助找到执行此操作的方法。
实现这个只是检查是否为空:
DocumentSnapshot document = task.getResult();
if (document != null) {
Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());
//Do the registration
} else {
Log.d(TAG, "No such document");
}
Task
的结果是 DocumentSnapshot
。通过 exists() 方法可以获得基础文档是否实际存在。
如果文档确实存在,您可以调用 getData 来获取它的内容。
db.collection("Companies").document(IDCOMPANIE1)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
if(document.exists()) {
//Do something
} else {
//Do something else
}
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
如果您想知道 task
是否为空,请使用以下代码行:
boolean isEmpty = task.getResult().isEmpty();
示例:
在使用事件调度功能之前,我必须先与 Firestore 进行预约。
Example in the database:
- Companie1 (Document)
--- name
--- phone
--- Schedules (Collection)
-----Event 1
-----Event 2
我有一个执行新计划的函数。
根据例子。 我需要检查 Schedules 集合是否存在。
如果不存在我执行调度函数。如果我已经存在,我需要做另一个程序。
我已经用过这个模式了,但不对。
db.collection("Companies").document(IDCOMPANIE1)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
在继续注册之前,我需要帮助找到执行此操作的方法。
实现这个只是检查是否为空:
DocumentSnapshot document = task.getResult();
if (document != null) {
Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());
//Do the registration
} else {
Log.d(TAG, "No such document");
}
Task
的结果是 DocumentSnapshot
。通过 exists() 方法可以获得基础文档是否实际存在。
如果文档确实存在,您可以调用 getData 来获取它的内容。
db.collection("Companies").document(IDCOMPANIE1)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
if(document.exists()) {
//Do something
} else {
//Do something else
}
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
如果您想知道 task
是否为空,请使用以下代码行:
boolean isEmpty = task.getResult().isEmpty();