func fetchPlansOperation(planUUIDs : [String], completion : @escaping([Plan],[Int]) -> Void) {
var plans = [Plan]()
var workoutCountArr = [Int]()
guard let uid = Auth.auth().currentUser?.uid else { return }
let queue = OperationQueue()
let fetchPlansOperation = BlockOperation {
for planUUID in planUUIDs {
REF_PLANS.child(planUUID).observe(.value) { snapshot in
guard let planDictionary = snapshot.value as? [String:Any] else { return }
let plan = Plan(uid : uid, planID: planUUID, dictionary: planDictionary)
print("FETCHED PLANS")
plans.append(plan)
}
}
}
let fetchWorkoutCountOperation = BlockOperation {
for planUUID in planUUIDs {
REF_PLANS.child(planUUID).child("workouts").observe(.value) { snapshot in
let workoutCount = snapshot.children.allObjects.count
print("FETCHED WORKOUTS")
workoutCountArr.append(workoutCount)
}
}
}
queue.addOperations([fetchPlansOperation,fetchWorkoutCountOperation], waitUntilFinished: true)
print("ITS OVER..")
}
The above code gives me this output :
ITS OVER.
FETCHED WORKOUTS
FETCHED PLANS
I want the Firebase fetch to be over and then print the "ITS OVER" statement. I need those values to perform further API fetches. What am I doing wrong here.
I know BlockOperations are asynchronous, meaning I have control over what starts but not what finishes. Is there any way I can make the Block Operation synchronous. I need those operations to finish before I can do something else. I need to fetch those values and then print "COMPLETED". Any help will be appreciated.