我正在通过akka以多线程方式触发setvalue和Onchildadded侦听器,以首先在Firebase中推送聊天,然后等待onchildadd方法执行某项操作。但是面临两个问题: 1.有时不被调用,有时被两次调用。 我的代码如下:
DatabaseReference fromDatabaseReference = FirebaseDatabase
.getInstance().getReference(messagePath);
final DatabaseReference fromRef = fromDatabaseReference.push();
fromRef.setValue(Value, new CompletionListener() {
public void onComplete(DatabaseError firebaseError,
DatabaseReference firebase) {
Chat chat = new Chat().setfromUser(UUID)
.setMessageId(fromRef.getKey());
chatsRef.push().setValueAsync(chat);
}
});
fromDatabaseReference.addChildEventListener(new ChildEventListener() {
ChatMessage msgAdded;
public void onChildAdded(DataSnapshot snapshot,
String previousChildName) {
try {
msgAdded = snapshot.getValue(ChatMessage.class);
if(msgAdded.getFromUserType().equalsIgnoreCase("XXX"))
{
count.countDown();
testResult = checkAndVerifyRespone(msgAdded);
if(testResult)
{
stepResult = true;
}
createDocReport(conversation);
ref.removeValue(new CompletionListener(){@Override
public void onComplete(DatabaseError error, DatabaseReference ref) {
count.countDown();
}});
} }
}
现在,当此代码使用不同的参与者与akka执行时,有时不会触发添加子对象,有时它不会等待并调用其中的函数。