1

Is there any elegant way to dynamically add where condition to the query based on conditions? For e.g, the default query is

query = collectionReference
.whereField(SaleProperties.saledate.rawValue, isGreaterThanOrEqualTo: startDate)
.whereField(SaleProperties.saledate.rawValue, isLessThanOrEqualTo: endDate)
.whereField(SaleProperties.isapprovedbystoremanager.rawValue, isEqualTo: true)

and I have 4 different conditions, like, state, division, store, user. When these filters is/are selected, I need to add where condition accordingly. Lets assume, I have selected state then the query would be

query = collectionReference
    .whereField(SaleProperties.state.rawValue, isEqualTo: selectedState)
    .whereField(SaleProperties.saledate.rawValue, isGreaterThanOrEqualTo: startDate)
    .whereField(SaleProperties.saledate.rawValue, isLessThanOrEqualTo: endDate)
    .whereField(SaleProperties.isapprovedbystoremanager.rawValue, isEqualTo: true)

likewise, if I have selected state and division

    query = collectionReference
    .whereField(SaleProperties.state.rawValue, isEqualTo: selectedState)
    .whereField(SaleProperties.division.rawValue, isEqualTo: selectedDivision)
    .whereField(SaleProperties.saledate.rawValue, isGreaterThanOrEqualTo: startDate)
    .whereField(SaleProperties.saledate.rawValue, isLessThanOrEqualTo: endDate)                 
   .whereField(SaleProperties.isapprovedbystoremanager.rawValue, isEqualTo: true)

Above example is illustrated just for two conditions. Like this, I need to check for 4 different conditions with combination of others.The probability of combination will be like 24 queries. So It would be great if someone knows the elegant way to form the queries.

*********Answer********

With the default query (please see above the default query), I check for the condition, example, if stateis selected, then I just add one more condition to the default query

    if(isStateSelected == true)
    {
   query =  query.whereField(SaleProperties.state.rawValue, isEqualTo: selectedState)
    }

likewise, if division is selected

    if(isDivisionSelected == true)
    {
  query = query.whereField(SaleProperties.division.rawValue, isEqualTo: selectedDivision)
    }

so if conditions are true, additional condition will be added to the default query

user3310076
  • 133
  • 12
  • So you say that you'll need to query your database based on 24 conditions? – Alex Mamo Nov 22 '18 at 12:32
  • I do not think you can combine range operators with firebase – J. Doe Nov 22 '18 at 12:43
  • I've marked this as a dup of the JavaScript solution, because they are essentially identical. But I don't think there is nothing "elegant" about it. You are basically obliged to re-assign a new Query object every time you want to add another conditional filter to the query using whereField(). Just keep building and assigning until you're done. – Doug Stevenson Nov 22 '18 at 12:55
  • @DougStevenson - Thanks for referring the link. It helped me build the query based on condition. – user3310076 Nov 23 '18 at 06:23
  • 1
    @AlexMamo Initially i thought so, but after seeing the doug's reference link, there is no need to for 24 conditions, just 4 is enough – user3310076 Nov 23 '18 at 06:25
  • For those who are looking for same kind of solution, i have updated my question with the answer – user3310076 Nov 23 '18 at 06:26

0 Answers0