The following data in the Firebase Database:
{
"schedule": {
"Week 1": {
"active": "true"
},
"Week 2": {
"active": "true"
},
"Week 3": {
"active": "false"
},
"Week 4": {
"active": "true"
},
...
}
}
I want to retrieve all the Weeks where the active is true only. I am setting the Firebase reference as such:
ref = FIRDatabase.database().reference(withPath: "Schedule")
and then doing the following:
ref.observeSingleEvent(of: .value, with: { snapshot in
//
print("Count: ", snapshot.childrenCount)
for _ in snapshot.children {
print("\(snapshot.value)")
}
})
Which produces the following output:
Optional({
"Week 1" = {
active = 1;
};
"Week 2" = {
active = 1;
};
"Week 3" = {
active = 0;
};
"Week 4" = {
active = 1;
};
...
}
Can someone please suggest how I can get just the weeks where active is set to true which needs to be loaded in an array of type string :
var weeksActive = [String]()
Here Is The Code
1) Create New Swift Page in The Name as
Schedule.swift
2) Inside
Schedule.swift
Page Use The Below Code3) In
ViewController.Swift
Page or Other Page Use This CodeHere I Have Added Code Upto Appending Data's To Array You Just Need To Validate Values As Per Your Needs Before Appending To ScheduleList Array.
This is the code for the problem mentioned above. First take the item value as FIRDatabaseSnapshot.value!. Then append it into weeksActive.
For that you can use
queryOrdered(byChild:)
andqueryEqual(toValue:)
like this.