gpt4 book ai didi

java - Firebase实时数据库使用Childeventlistener分页

转载 作者:行者123 更新时间:2023-12-02 10:50:30 24 4
gpt4 key购买 nike

我正在使用 Firebase 实时数据库分页检查我的以下数据,并发现了一个奇怪的行为。

考虑服务器中的以下数据,用户可以从客户端将列表添加到其中:

"USER1" : {
"-LLmMMPQZNNkpNkIgTpv" : {
"id" : 1001,
"name" : "PERSON1001"
},
"-LLmPja-ku3bVPUvbA73" : {
"id" : 1002,
"name" : "PERSON1002"
},
"-LLmPjdPvC82jnL1D6h2" : {
"id" : 1003,
"name" : "PERSON1003"
},
"-LLmPjgCq-2D4sldLHY6" : {
"id" : 1004,
"name" : "PERSON1004"
},
"-LLmPjiyZpm41wMa9t37" : {
"id" : 1005,
"name" : "PERSON1005"
},
"-LLmPjllgr-1ZKjR-u35" : {
"id" : 1006,
"name" : "PERSON1006"
},
"-LLmPjoS1pqhhKsiiHf8" : {
"id" : 1007,
"name" : "PERSON1007"
},
"-LLmPjqyY8-mJRy4L-so" : {
"id" : 1008,
"name" : "PERSON1008"
},
"-LLmPjtXwLslDQ6T9s8X" : {
"id" : 1009,
"name" : "PERSON1009"
},
"-LLmPjw25hnojgjHRlvj" : {
"id" : 1010,
"name" : "PERSON1010"
},
"-LLmPjyk3pG13CeOvMxJ" : {
"id" : 1011,
"name" : "PERSON1011"
},
"-LLmPk0X83DahACj4sCC" : {
"id" : 1012,
"name" : "PERSON1012"
},
"-LLmPk2vkUlimmvqI7aI" : {
"id" : 1013,
"name" : "PERSON1013"
},
"-LLmPk5YXhPVKHSTBnW2" : {
"id" : 1014,
"name" : "PERSON1014"
},
"-LLmPk806MCTpnxRs_62" : {
"id" : 1015,
"name" : "PERSON1015"
},
"-LLmPkATgrrguNt4iEyn" : {
"id" : 1016,
"name" : "PERSON1016"
},
"-LLmPkD82ua0YHoqatOV" : {
"id" : 1017,
"name" : "PERSON1017"
},..,..,..
}

......

public class Database {

private String TAG = Database.class.getSimpleName();
private FirebaseDatabase database;
private int pagingCounter = 10;
private int totalCount = 0;
private int totalItemsToLoad;
private Query query;
private int createNewCounter = 2001;

public Database(){
database = FirebaseDatabase.getInstance();
}

private DatabaseReference getDatabaseReference(){
return database.getReference("EMPLOYEE").child("USER1").getRef();
}

private Query getQueryIntial(){
return getDatabaseReference().orderByChild("id").limitToFirst(pagingCounter);
}

private Query getReferenceFromRange(int start){
return getDatabaseReference().orderByChild("id").startAt(start).endAt(9999).limitToFirst(pagingCounter);
}

public void getPages(){
Query myRef = getQueryIntial();
getDatabase(myRef);
}

private void getNextPage(int id) {
getDatabase(getReferenceFromRange(id));
}

public void getDatabase(final Query q) {
this.query = q;
totalItemsToLoad=totalItemsToLoad+pagingCounter;
final ArrayList<Integer> ids = new ArrayList();
query.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Person person = dataSnapshot.getValue(Person.class);
if(person!= null) {
Log.d(TAG, "ONCHILDADDED " + dataSnapshot);
ids.add(person.getId());
}
totalCount=totalCount+1;
if(totalCount>=totalItemsToLoad)
{
query.removeEventListener(this);
int id = Collections.max(ids);
getNextPage(id);
}
}

@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

}

@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}

public void createNew() {
String push = getDatabaseReference().push().getKey();
Person person = new Person();
person.setValue(createNewCounter);
createNewCounter=createNewCounter+1;
getDatabaseReference().child(push).setValue(person).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(!task.isSuccessful()) {
Exception e = task.getException();
if (e != null) {
e.printStackTrace();
}
}
}
});
}

}

但是当从客户端添加下一个人员时出现问题,我从客户端添加了 pageid ,其 id 为 2001 年......,然后分页结果为

 //1st page
CHILD_ADDED: { -LLmMMPQZNNkpNkIgTpv: {name=PERSON1001, id=1001} }
CHILD_ADDED: { -LLmPja-ku3bVPUvbA73: {name=PERSON1002, id=1002} }
CHILD_ADDED: { -LLmPjdPvC82jnL1D6h2: {name=PERSON1003, id=1003} }
CHILD_ADDED: { -LLmPjgCq-2D4sldLHY6: {name=PERSON1004, id=1004} }
CHILD_ADDED: { -LLmPjiyZpm41wMa9t37: {name=PERSON1005, id=1005} }
CHILD_ADDED: { -LLmPjllgr-1ZKjR-u35: {name=PERSON1006, id=1006} }
CHILD_ADDED: { -LLmPjoS1pqhhKsiiHf8: {name=PERSON1007, id=1007} }
CHILD_ADDED: { -LLmPjqyY8-mJRy4L-so: {name=PERSON1008, id=1008} }
CHILD_ADDED: { -LLmPjtXwLslDQ6T9s8X: {name=PERSON1009, id=1009} }
CHILD_ADDED: { -LLmPjw25hnojgjHRlvj: {name=PERSON1010, id=1010} }
//2nd page query from 1010 to 9999 to get first 10 perso ns
CHILD_ADDED: { -LLmPjw25hnojgjHRlvj: {name=PERSON1010, id=1010} }
CHILD_ADDED: { -LLmOIlxQ_v2dSDSKgQ8: {name=PERSON2001, id=2001} }
CHILD_ADDED: { -LLmOIq4IHOT_LEYbpLt: {name=PERSON2002, id=2002} }
CHILD_ADDED: { -LLmOItgo5jG7dNTmfIR: {name=PERSON2003, id=2003} }
CHILD_ADDED: { -LLmReS7j6Gtz7Q_4LPQ: {name=PERSON2004, id=2004} }
CHILD_ADDED: { -LLmReUhQtB6xiTOfufE: {name=PERSON2005, id=2005} }
CHILD_ADDED: { -LLmReXPmHlHj8Ai0gow: {name=PERSON2006, id=2006} }
CHILD_ADDED: { -LLmReZlWyCI1C4u-2-0: {name=PERSON2007, id=2007} }
CHILD_ADDED: { -LLmRebJx6-DGemO_F34: {name=PERSON2008, id=2008} }
CHILD_ADDED: { -LLmRedpz5mCOc365Pvu: {name=PERSON2009, id=2009} }
//3nd page
CHILD_ADDED: { -LLmRedpz5mCOc365Pvu: {name=PERSON2009, id=2009} }

但是,如果客户端没有添加子项,结果将正常工作。

最佳答案

这是预期的行为。将为与您的查询匹配的每个子节点调用 onChildAdded() 方法。因此,如果您有 2009 个项目,onChildAdded() 方法将被调用 2009 次。最重要的是,每次添加新子项时也会调用此方法。这也意味着每次添加新的子监听器时,您都会添加一个新的子监听器。因此,如果您提交两个人,您就有两个监听器,并且您的 onChildAdded() 将为每个子人调用两次(每个监听器一次)。然后,当您提交另一个监听器时,它会添加第三个监听器。等等。

此问题的解决方案是在附加新监听器之前分离前一个监听器。这是关于 detaching listeners 的官方文档.

关于java - Firebase实时数据库使用Childeventlistener分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52216030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com