I want to update real time data in my app. I have changed the code but problem is 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();
}
})
;
}
my old code... sir can u Get realtime updates with Cloud Firestore? this code isn't working in realtime.. it works only whenever i restart/refresh the app. i am using cloud Firestore. my java is not good please sir u change this code in real-time...
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();
}
})
;
}
Calling query.addSnapshotListener
does not give you a task, but rather an event. The equivalent of your if (task.isSuccessful()) {
within onEvent
would be to check if (error == null) {
, and the equivalent of addOnFailureListener
would be to the else
of the that condition.
If you really want to use a Task
, you can call query.get
instead of query.addSnapshotListener
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.