这是我第一次使用堆栈溢出。我试图让我的程序在继续前等待查询任务完成,但我从来没有得到它的行为正确。我在打印报表中进行调试。第二次和第三次打印总是在第一次打印之前出现。我认为我不太了解dispatch_sync。任何帮助将不胜感激!如何在swift中使用dispatch_sync?
let concurrentQueue = dispatch_queue_create(
"com.x.TinderClone.queue", DISPATCH_QUEUE_CONCURRENT)
dispatch_sync(concurrentQueue) {
usersRef.queryOrderedByChild("gender").queryEqualToValue(userInterestedIn).observeSingleEventOfType(.Value, withBlock: {snapshot in
for child in snapshot.children {
let uid = child.key!!
let gender = child.value!!["gender"] as! String
let id = child.value!!["id"] as! String
let interestedInWomen = child.value!!["interestedInWomen"] as! Bool
let name = child.value!!["name"] as! String
let potentialMatch = User(uid: uid, gender: gender, id: id, interestedInWomen: interestedInWomen, name: name)
listOfPotentialMatches.append(potentialMatch) //add the user struct to the array
}
print("First") // First print
// ...
}) { (error) in
print(error.localizedDescription)
}
print("Second") // Second Print
}
print("Third") //Third print