The second call needs to come after the first call per my comment. Firebase is already asynchronous and to ensure things happen in sequence they need to be treated as such within the Firebase closure. So a typical pattern is
addObserver {
receive a valid snapshot {
do something with snapshot data as it's only valid at this point
}
}
Also, we can do this entirely in Swift and since you know what the node names are, they can be accessed directly instead of working through a NSDictionary. I've also include basic error checking to catch nil nodes.
var didntPractiseInt = 0
func checkDidntPracticeCount() {
let whichNodeRef = self.ref.child("practice").child("some_node")
let didntPracticeCounterRef = whichNodeRef.child("didntPracticeCounter")
didntPracticeCounterRef.observeSingleEvent(of: .value, with: { snapshot in
if let didntPracticeCount = Int(snapshot.value as! String) {
self.didntPractiseInt = didntPracticeCount
self.checkDidPracticeOnADay()
} else {
print("didntPracticeCounter node not found")
}
})
}
func checkDidPracticeOnADay() {
let whichNodeRef = self.ref.child("practice").child("some_node")
let dayNum = "0"
let whichDayRef = whichNodeRef.child("days").child(dayNum).child("practiced")
whichDayRef.observeSingleEvent(of: .value, with: { snapshot in
if let didPractice = snapshot.value as? String {
if didPractice == "false" {
self.didntPractiseInt += 1
print(self.didntPractiseInt)
} else {
print("did practice on day \(dayNum)")
}
}
})
}
Note that self.ref points to my Firebase as a class var.
I would also suggest using boolean values instead of "false" and "true" and you may want to consider storing your counters as Int's instead of String's as they may be a bit easier to work with for sorting etc.
Lastly, array's can be bad news in NoSQL databases and their use is very situational. You may want to consider storing your days some other way. It may be ok in this case but please take a look at Arrays Are Evil. It's 2014 post but still very applicable.
Edit:
I was thinking about this and given there will always be 7 days in your days node, there may be a simpler approach. I no idea if this well help but a simple query will provide a count of how many days a user practiced during a given week.
let daysRef = self.ref.child("practice").child("some_node").child("days")
let query = daysRef.queryOrdered(byChild: "practiced").queryEqual(toValue: "true")
query.observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.childrenCount)
})
If that's the case then all of the code in the first part in my answer could be replaced with this code.