0

I want to filter out the all questions where isValid flag is set to false, and want to retrieve in order of question count.

This is sample Data in firebase.

Question: {
    Key1: {
        text:"Text1",
        isValid:true,
        count:1
    },
    Key2:{
        text:"Text2",
        isValid:false,
        count:2
    },
    Key3:{
        text:"Text3",
        isValid:true,
        count:3
    },
    Key4:{
        text:"Text4",
        isValid:false,
        count:4
    },
    Key5:{
        text:"Text5",
        isValid:true,
        count:5
    },
    Key6:{
        text:"Text6",
        isValid:true,
        count:6
    }
    ...
},
User: {
    Key1: {
        name:"User1",
        lastValue:1
    },
    Key2: {
        name:"User2",
        lastValue:3
    }
}

User get 2 question daily and his lastValue is updated. I have tried below approach but its only giving the next two questions. I want to filter out the invalid question.

let QUESTION_PATH = "Question"
let LASTVALUE = 1

Database.database().reference().child(QUESTION_PATH).queryOrdered(byChild: "count").queryStarting(atValue: LASTVALUE).queryLimited(toFirst: 2).observeSingleEvent(of: .value, with: {(snapshot) in
    //I want to Get the only next two valid question. in Above Example it should give 3 and 5. But this query will give 2 and 3.
 })
Ritesh Chandora
  • 8,382
  • 5
  • 21
  • 38
  • Firebase Database can only order/filter on a single property. Luckily you can combine the two values you want to use here into a single synthetic property and filter/order on that, e.g. `"isValid_count": "true_6"`. See my answer here for more: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Nov 14 '17 at 15:58
  • @FrankvanPuffelen I need those question in order by Count of questions. – Ritesh Chandora Nov 14 '17 at 16:00
  • Check the last example in solution 2, which does precisely that. For you it'd be `queryOrdered(byChild: "isValid_count").queryStarting(atValue: "false_")`. – Frank van Puffelen Nov 14 '17 at 16:07

0 Answers0