如何将 Tasks 与 Firestore onSnapshotListener 结合使用?
How can I use Tasks with Firestore onSnapshotListener?
我想在我的应用程序中更新实时数据。我已经更改了代码,但问题是 Task Cannot Find
.
private void getData() {
showProgressDialog();
Query query;
if (islast) {
query = db.collection(node)
.document(lastid)
.collection("list")
.document(id)
.collection("list");
} else {
query = db.collection(node)
.document(id)
.collection("list");
}
query.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
dismissProgressDialog();
if (task.isSuccessful()) {
list.clear();
int i = 0;
boolean match = false;
try {
for (DocumentSnapshot snapshot : task.getResult().getDocuments()) {
CardModel mainModel = snapshot.toObject(CardModel.class);
mainModel.setId(snapshot.getId());
if (mainModel.enable) {
boolean onOff=mainModel.cOnOff;
if (mainModel.countries != null
&& mainModel.countries.size() > 0) {
if (!sessionManager.getLocation().isEmpty()) {
if(onOff) {
if (mainModel.countries.contains(sessionManager.getLocation())) {
match = true;
list.add(mainModel);
} else {
list.remove(mainModel);
}
}else{
if (mainModel.countries.contains(sessionManager.getLocation())) {
match = true;
list.remove(mainModel);
} else {
list.add(mainModel);
}
}
} else {
list.add(mainModel);
}
} else {
list.add(mainModel);
}
} else {
list.remove(mainModel);
}
Log.d("view imgs", "data is " + mainModel.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}
adapter.notifyDataSetChanged();
dismissProgressDialog();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
dismissProgressDialog();
Toast.makeText(Category_List_Activity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
;
}
我的旧代码...先生,您可以使用 Cloud Firestore 获取实时更新吗?此代码无法实时工作。它仅在我 restart/refresh 应用程序时有效。我正在使用云 Firestore。我的 java 不好请先生实时更改此代码...
private void getData() {
showProgressDialog();
Query query;
if (islast) {
query = db.collection(node)
.document(lastid)
.collection("list")
.document(id)
.collection("list");
} else {
query = db.collection(node)
.document(id)
.collection("list");
}
query.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
dismissProgressDialog();
if (task.isSuccessful()) {
list.clear();
int i = 0;
boolean match = false;
try {
for (DocumentSnapshot snapshot : task.getResult().getDocuments()) {
CardModel mainModel = snapshot.toObject(CardModel.class);
mainModel.setId(snapshot.getId());
if (mainModel.enable) {
boolean onOff=mainModel.cOnOff;
if (mainModel.countries != null
&& mainModel.countries.size() > 0) {
if (!sessionManager.getLocation().isEmpty()) {
if(onOff) {
if (mainModel.countries.contains(sessionManager.getLocation())) {
match = true;
list.add(mainModel);
} else {
list.remove(mainModel);
}
}else{
if (mainModel.countries.contains(sessionManager.getLocation())) {
match = true;
list.remove(mainModel);
} else {
list.add(mainModel);
}
}
} else {
list.add(mainModel);
}
} else {
list.add(mainModel);
}
} else {
list.remove(mainModel);
}
Log.d("view imgs", "data is " + mainModel.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}
adapter.notifyDataSetChanged();
dismissProgressDialog();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
dismissProgressDialog();
Toast.makeText(Category_List_Activity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
;
}
调用 query.addSnapshotListener
不会给你一个任务,而是一个事件。 onEvent
中的 if (task.isSuccessful()) {
相当于检查 if (error == null) {
,而 addOnFailureListener
相当于该条件的 else
。
如果你真的想使用 Task
,你可以调用 query.get
而不是 query.addSnapshotListener
。
我想在我的应用程序中更新实时数据。我已经更改了代码,但问题是 Task Cannot Find
.
private void getData() {
showProgressDialog();
Query query;
if (islast) {
query = db.collection(node)
.document(lastid)
.collection("list")
.document(id)
.collection("list");
} else {
query = db.collection(node)
.document(id)
.collection("list");
}
query.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
dismissProgressDialog();
if (task.isSuccessful()) {
list.clear();
int i = 0;
boolean match = false;
try {
for (DocumentSnapshot snapshot : task.getResult().getDocuments()) {
CardModel mainModel = snapshot.toObject(CardModel.class);
mainModel.setId(snapshot.getId());
if (mainModel.enable) {
boolean onOff=mainModel.cOnOff;
if (mainModel.countries != null
&& mainModel.countries.size() > 0) {
if (!sessionManager.getLocation().isEmpty()) {
if(onOff) {
if (mainModel.countries.contains(sessionManager.getLocation())) {
match = true;
list.add(mainModel);
} else {
list.remove(mainModel);
}
}else{
if (mainModel.countries.contains(sessionManager.getLocation())) {
match = true;
list.remove(mainModel);
} else {
list.add(mainModel);
}
}
} else {
list.add(mainModel);
}
} else {
list.add(mainModel);
}
} else {
list.remove(mainModel);
}
Log.d("view imgs", "data is " + mainModel.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}
adapter.notifyDataSetChanged();
dismissProgressDialog();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
dismissProgressDialog();
Toast.makeText(Category_List_Activity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
;
}
我的旧代码...先生,您可以使用 Cloud Firestore 获取实时更新吗?此代码无法实时工作。它仅在我 restart/refresh 应用程序时有效。我正在使用云 Firestore。我的 java 不好请先生实时更改此代码...
private void getData() {
showProgressDialog();
Query query;
if (islast) {
query = db.collection(node)
.document(lastid)
.collection("list")
.document(id)
.collection("list");
} else {
query = db.collection(node)
.document(id)
.collection("list");
}
query.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
dismissProgressDialog();
if (task.isSuccessful()) {
list.clear();
int i = 0;
boolean match = false;
try {
for (DocumentSnapshot snapshot : task.getResult().getDocuments()) {
CardModel mainModel = snapshot.toObject(CardModel.class);
mainModel.setId(snapshot.getId());
if (mainModel.enable) {
boolean onOff=mainModel.cOnOff;
if (mainModel.countries != null
&& mainModel.countries.size() > 0) {
if (!sessionManager.getLocation().isEmpty()) {
if(onOff) {
if (mainModel.countries.contains(sessionManager.getLocation())) {
match = true;
list.add(mainModel);
} else {
list.remove(mainModel);
}
}else{
if (mainModel.countries.contains(sessionManager.getLocation())) {
match = true;
list.remove(mainModel);
} else {
list.add(mainModel);
}
}
} else {
list.add(mainModel);
}
} else {
list.add(mainModel);
}
} else {
list.remove(mainModel);
}
Log.d("view imgs", "data is " + mainModel.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}
adapter.notifyDataSetChanged();
dismissProgressDialog();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
dismissProgressDialog();
Toast.makeText(Category_List_Activity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
;
}
调用 query.addSnapshotListener
不会给你一个任务,而是一个事件。 onEvent
中的 if (task.isSuccessful()) {
相当于检查 if (error == null) {
,而 addOnFailureListener
相当于该条件的 else
。
如果你真的想使用 Task
,你可以调用 query.get
而不是 query.addSnapshotListener
。