I don't want to refresh recyclerview items when a new data is added in firestore database. Firestore returns all documents from Student collection every time a new student is added in Firestore database. How should I prevent firestore from returning all documents when a new student is added?
Here is my code:
private void load() {
Query query = createQuery();
FirestoreRecyclerOptions<Student> options = new FirestoreRecyclerOptions.Builder<Student>()
.setQuery(query, Student.class)
.build();
myStudentAdapter = new MyStudentAdapter(options);
rv.setAdapter(myStudentAdapter);
myStudentAdapter.startListening();
}
private Query createQuery() {
if (!search.isEmpty()) {
if (!course.isEmpty() && !year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course)
.whereEqualTo("year", year)
.whereEqualTo("block", block)
.startAt(search)
.endAt(search '\uf8ff');
} else if (!course.isEmpty() && !year.isEmpty() && block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course)
.whereEqualTo("year", year)
.startAt(search)
.endAt(search '\uf8ff');
} else if (!course.isEmpty() && year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course)
.whereEqualTo("block", block)
.startAt(search)
.endAt(search '\uf8ff');
} else if (course.isEmpty() && !year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("year", year)
.whereEqualTo("block", block)
.startAt(search)
.endAt(search '\uf8ff');
} else if (!course.isEmpty() && year.isEmpty() && block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course)
.startAt(search)
.endAt(search '\uf8ff');
} else if (course.isEmpty() && !year.isEmpty() && block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("year", year)
.startAt(search)
.endAt(search '\uf8ff');
} else if (course.isEmpty() && year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("block", block)
.startAt(search)
.endAt(search '\uf8ff');
} else {
return db.collection("Student")
.orderBy("fullName")
.startAt(search)
.endAt(search '\uf8ff');
}
} else {
if (!course.isEmpty() && !year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course)
.whereEqualTo("year", year)
.whereEqualTo("block", block);
} else if (!course.isEmpty() && !year.isEmpty() && block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course)
.whereEqualTo("year", year);
} else if (!course.isEmpty() && year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course)
.whereEqualTo("block", block);
} else if (course.isEmpty() && !year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("year", year)
.whereEqualTo("block", block);
} else if (!course.isEmpty() && year.isEmpty() && block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("course", course);
} else if (course.isEmpty() && !year.isEmpty() && block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("year", year);
} else if (course.isEmpty() && year.isEmpty() && !block.isEmpty()) {
return db.collection("Student")
.orderBy("fullName")
.whereEqualTo("block", block);
} else {
return db.collection("Student")
.orderBy("fullName");
}
}
}
@Override
public void onStart() {
super.onStart();
myStudentAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
myStudentAdapter.stopListening();
}
CodePudding user response:
The adapters in the FirebaseUI library: they monitor the data on the server, and update to reflect changes in that data. There is no way to change the behaviors of these adapters to only load the data, and skip the monitoring.
If you don't want to reflect changes, you'll have to implement your own adapter.