簡體   English   中英

使用Firebase Android API通過嵌套數據排序查詢時發生異常

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM