In the following code, the function will return "Var" before the checkDayEqualsToDoc function can return a bool and change "Var". I've tried looking a lot of places for an answer and I keep seeing the same thing... use closures... use completions... etc. Are those the correct things to use in this case? And if so, how would I implement that?
func dotMarker(shouldShowOnDayView dayView: DayView) -> Bool {
var Var:Bool = false
let dDate = dayView.date.convertedDate()
DispatchQueue.main.async {
self.checkDayEqualsToDoc(date: dayView.date.convertedDate()!){ (bool, error) in
Var = bool
}
}
return Var
}
Here is the checkDayEqualsToDoc function:
func checkDayEqualsToDoc(date: Date, completionHandler:@escaping (Bool, Error?) -> Void){
var Variable = false
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: date)
let btRef = db.collection("bomtracking")
let myDate = date
let startOfDate = myDate.startOfDay
let endOfDate = myDate.endOfDay
let dateAtBeginning = date.firstSecondInDay()
let dateAtEnd = date.lastSecondInDay()
let query = btRef
.whereField("UID", isEqualTo: UID)
.whereField("timestamp", isGreaterThan: dateAtBeginning)
.whereField("timestamp", isLessThan: dateAtEnd)
DispatchQueue.main.async {
query.getDocuments(){ (querySnapshot, err) in
if let err = err {
print("error getting documents: \(err)")
} else {
for document in querySnapshot!.documents{
let curValid = document.get("valid")
let curValidA = Bool(curValid as! Bool)
if curValidA == true {
let curdoc = document.get("timestamp")
let curdocDate = self.dateConversion(curdoc as! Date)
let dateConverted = self.dateConversion(date)
if curdocDate == dateConverted {
Variable = true
break
}
else {
Variable = false
}
}
}
completionHandler(Variable, nil)
}
}
}
}
}