我正在用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} }
但是,如果没有从客户端添加任何子级,则结果可以正常工作。
答案 0 :(得分:1)
这是预期的行为。将为与您的查询匹配的每个子节点调用onChildAdded()
方法。因此,如果您有2009年的商品,则onChildAdded()
方法将被称为2009年。最重要的是,每次添加新孩子时,也会调用此方法。这也意味着,每次添加新的孩子时,您都要添加一个新的孩子侦听器。因此,如果您提交两个人,则您有两个侦听器,您的onChildAdded()
将为每个孩子呼叫两次(每个侦听器一次)。然后,当您提交另一个监听器时,它将添加第三个监听器。依此类推。
此问题的解决方案是在附加新的侦听器之前先分离先前的侦听器。这是有关detaching listeners的官方文档。