![](/img/trans.png)
[英]Firebase Query - Accessing nested object in Firebase Android
[英]Exception when ordering query by nested data with Firebase Android API
根据Firebase文档,应该可以通过将完整路径传递给orderByChild()
方法来对深度嵌套的子项进行查询排序
Firebase ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
Query queryRef = ref.orderByChild("dimensions/height");
queryRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChild) {
DinosaurFacts facts = snapshot.getValue(DinosaurFacts.class);
System.out.println(snapshot.getKey() + " was " + facts.getHeight() + " meters tall");
}
});
但是,调用orderByChild("dinosaurs/height")
会引发FirebaseException
,并显示消息Invalid key: dinosaurs/height. Keys must not contain '/', '.', '#', '$', '[', or ']'
Invalid key: dinosaurs/height. Keys must not contain '/', '.', '#', '$', '[', or ']'
。
实际上,该密钥被以下firebase方法拒绝
private static final Pattern INVALID_KEY_REGEX = Pattern.compile("[\\[\\]\\.#\\$\\/\\u0000-\\u001F\\u007F]");
private static boolean isValidKey(String key) {
return key.equals(".info") || !INVALID_KEY_REGEX.matcher(key).find();
}
使用对象的完整路径进行查询的正确方法是什么?
Firebase SDK for Java 2.4版中增加了通过嵌套子项进行订购的功能。
我只是尝试在旧版SDK上使用/
,并得到与您相同的错误。 因此,我最好的选择是,您忘记了更新项目以使用Firebase SDK 2.4版。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.