简体   繁体   中英

A simple filter for Firebase query doesn't work

I'm trying to make a filter for the posts, where user can choose posts from which city or district they want to look at. Also there are three different categories of posts. (cat_a, cat_b, cat_c).

switch (category) {
    case 0:
        // if category is 0, then show all posts
        mQuery = db.getReference("post");
        break;
    case 1:
        mQuery = db.getReference("post").orderByChild("category").equalTo("cat_a");
        break;
    case 2:
        if (cityPref == 0) {
             mQuery = db.getReference("post")
                        .orderByChild("category")
                        .equalTo("cat_b");
        } else {
             mQuery = db.getReference("post")
                        .orderByChild("categoryCity")
                        .equalTo("cat_b" + cityString);
        }
        break;
    case 3:
        if (cityPref == 0) {
             mQuery = db.getReference("post")
                        .orderByChild("category")
                        .equalTo("cat_c");
        } else if (districtPref == 0) {
             mQuery = db.getReference("post")
                        .orderByChild("categoryCity")
                        .equalTo("cat_c" + cityString);
        } else {
             mQuery = db.getReference("post")
                        .orderByChild("categoryDistrict")
                        .equalTo("cat_c" + district);
        }
        break;
    default:
        mQuery = db.getReference("post");
}

Interestingly, it's not that all of the queries are not working, but for each of categories of posts, more specific queries all all posts work. Following Queries are working fine.

 mQuery = db.getReference("post");
 mQuery = db.getReference("post")
            .orderByChild("categoryCity")
            .equalTo("cat_b_" + cityString);
 mQuery = db.getReference("post")
            .orderByChild("categoryDistrict")
            .equalTo("cat_c_"+ city + "_" + district);

All the posts that I query using category just doesn't work... and I'm 100% sure that FirebaseDatabase has the category in the data. Also, I've tried putting Log.v(...) to check if all the places inside the switch statement and if statements are reached. I would really appreciate any help!

Just for reference, this is my data structure from Firebase:

"post": {
    "post_uid_1": {
        "category": "cat_a",
        "categoryCity": "cat_a_seoul",
        "categoryDistrict": "cat_a_seoul_songpa",
        "city": "seoul",
        "context": "this is context",
        "district": "songpa",
        .
        .
        .
    }
    "post_uid_2": {
        .
        .
        .
    }
}

I've solved my own problem - if anyone's having the same problem, you can make query work effectively by indexing the data on Firebase rule tab. https://firebase.google.com/docs/database/security/indexing-data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM