我有一个class Base {
public:
Base() {};
virtual ~Base() {};
};
class Int : public Base {
public:
Int(int i) {
this->i = i;
}
Int operator=(int i) {
this->i = i;
}
int i;
};
void set() {
map<string, unique_ptr<Base>> variables;
variables.insert(make_pair("test", new Int(10)));
}
节点和一个Order
密钥。我正在寻找Status
值的更改,如果检测到任何更改,我必须Status
父read
节点。
关键是我不想听Order
节点的变化。但只是Order
节点下的Status
。最佳方式是什么?在另一个监听器中有一个监听器是否好?这是我的代码:
Order
JSON:
final Query qRef = FirebaseDatabase.getInstance().getReference().child("orders").orderByChild("customerId").equalTo(FirebaseAuth.getInstance().getCurrentUser().getUid())
.limitToFirst(1);
qRef.keepSynced(true);
final DatabaseReference reference = qRef.getRef().child("status");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DatabaseReference inner = reference.getParent();
inner.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot parent) {
if (parent != null) {
Order newOrder = parent.getValue(Order.class);
if (newOrder != null) {
if (newOrder.getStatus() != null && !newOrder.getStatus().equals("FX_OVER"))
processOrder(newOrder);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
答案 0 :(得分:2)
为什么不先检查状态?你知道开始状态的价值。我认为这个解决方案应该是最佳方式。
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if("FX_OVER".equals(dataSnapshot.getValue(String.class)) {
reference.getParent().addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot parent) {
// This is a defensive check. I dont know this is necessary.
// If status exist, parent must be exist.
if (parent.exists()) {
Order newOrder = parent.getValue(Order.class);
if (newOrder != null) {
processOrder(newOrder);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});