下面是我用来在recyclerview中检索文档数据的代码。工作正常。但是,无论何时添加新文档,它都不会实时更新。我知道快照侦听器仅用于此目的,但很难使其工作。任何帮助将不胜感激。 :)
mFirestore.collection("Users").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
for (DocumentSnapshot document : task.getResult()) {
Message message = document.toObject(Message.class);
messageList.add(message);
mAdapter.notifyDataSetChanged();
}
}
}
});
答案 0 :(得分:2)
您应该像这样将快照事件分开。然后您可以轻松找出问题所在
function handlePlayer (username, res) {
database.getPlayerExistsByName(username).then(result => {
if (result.length != 1) { // Username does not exist
database.insertPlayer(username) // Insert username
database.getPlayerByName(username).then(penguin => {
return write(`&id=${penguin.id}&m=${penguin.mod}${Errors.OK}`, res) // I need the id here
})
} else {
return write(Errors.NAME_UNAVAILABLE, res)
}
})
}
也许您获取的快照是由[MODIFIED]事件而非[ADDED]事件触发的。
答案 1 :(得分:0)
如果您想继续收听Firestore数据(实时更新),则应这样做:
mFirestore.collection("Users")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w("YourTag", "Listen failed.", e);
return;
}
for (QueryDocumentSnapshot doc : queryDocumentSnapshots) {
if (doc.exists()){
Message message = doc.toObject(Message.class);
messageList.add(message);
mAdapter.notifyDataSetChanged();
}
}
Log.d("YourTag", "messageList: " + messageList);
}
});
您使用的方式只能一次检索Firestore数据。
检查此>> https://firebase.google.com/docs/firestore/query-data/listen
答案 2 :(得分:0)
请查看如何在回收站视图中使用它:
// Get Chats List From FireStore
private void getChatListFromFireBase(String hiddenStatus) {
lIndividualChatList = new ArrayList<>();
lIndividualChatListIds = new ArrayList<>();
lIndividualChatList.clear();
lIndividualChatListIds.clear();
fbFs.collection("individual_chats")
.document(mySqlUserId)
.collection("lists")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots,
FirebaseFirestoreException e) {
if (e != null) {
}
for (DocumentChange dc : documentSnapshots.getDocumentChanges()) {
switch (dc.getType()) {
case ADDED:
key = dc.getDocument()
.getId();
firebaseRetrofitQuery(dc,
"Added",
key);
break;
case MODIFIED:
key = dc.getDocument()
.getId();
// Do The Change Function
firebaseRetrofitQuery(dc,
"Changed",
key);
break;
case REMOVED:
break;
}
}
}
});
}
// FireBase And Retrofit Query
private void firebaseRetrofitQuery(DocumentChange documentChange,
final String childActivityType,
final String key) {
if (childActivityType.equals("Added")) {
lIndividualChatListIds.add(key);
}
// Get The Receiver Id To Get Data From Other Nodes
final String mySqlFriendUserId = documentChange.getDocument()
.getId();
final String hideStatus = (String) documentChange.getDocument()
.getData()
.get("hide_status");
if (childActivityType.equals("Added")) {
// Populate The Array List With Data From Both Nodes
lIndividualChatList.add(new IndividualListModel(mySqlFriendUserId,
hideStatus));
}
if (childActivityType.equals("Changed")) {
int index = lIndividualChatListIds.indexOf(key);
lIndividualChatList.set(index,
new IndividualListModel(mySqlFriendUserId,
hideStatus));
}
// Use The Adapter To Populate The Recycler View
aIndividualChatList = new IndividualListAdapter(getContext(),
lIndividualChatList,
senderActivity,
new IndividualListAdapter.OnItemClickListener() {
@Override
public void onItemClicked(final Map data) {
}
});
rvList.setAdapter(aIndividualChatList);
aIndividualChatList.notifyDataSetChanged();
}