gpt4 book ai didi

ios - 同步调度不起作用?

转载 作者:行者123 更新时间:2023-11-28 13:17:18 26 4
gpt4 key购买 nike

也许我不理解这个概念,但第二段代码不应该在第一段代码完成之前等待吗?

dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> () in
// do some task
var myRef = self.userRef.childByAppendingPath(self.userRef.authData.uid)
myRef.observeSingleEventOfType(.Value, withBlock: { snapshot in
println("WE ARE IN HERE")
firstName = snapshot.value["firstName"] as String!
lastName = snapshot.value["lastName"] as String!
})
});

dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> () in
// do some task
var standbyData = [
"firstName":firstName,
"lastName":lastName,
"duration":self.helpDurationLabel.text!,
"radius":self.helpRadiusLabel.text!,
"categories": categories,
"time_stamp":self.date.timeIntervalSince1970
]
println("WE ARE OUT HERE")
var standbyUserRef = self.standbyRef.childByAppendingPath(self.userRef.authData.uid)
standbyUserRef.updateChildValues(standbyData)
});

有什么我想念的吗?

输出是:

"WE ARE OUT HERE"
"WE ARE IN HERE"

最佳答案

它会等到第一个完成执行。系统让您感到困惑的地方在于您对 myRef.observeSingleEventOfType(, withBlock:

的使用

99% 请求执行 block 的 API 会将其操作推送到单独的线程,然后在完成时执行该 block 。 observeSingleEventOfType 可能在将其执行安排到操作队列后不久返回,导致您的 dispatch_sync 在 block 执行之前退出。

相反,您可能正在寻找更像这样的东西:

dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> () in
// do some task
var myRef = self.userRef.childByAppendingPath(self.userRef.authData.uid)
myRef.observeSingleEventOfType(.Value, withBlock: { snapshot in
println("WE ARE IN HERE")
firstName = snapshot.value["firstName"] as String!
lastName = snapshot.value["lastName"] as String!

dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> () in
// do some task
var standbyData = [
"firstName":firstName,
"lastName":lastName,
"duration":self.helpDurationLabel.text!,
"radius":self.helpRadiusLabel.text!,
"categories": categories,
"time_stamp":self.date.timeIntervalSince1970
]
println("WE ARE OUT HERE")
var standbyUserRef = self.standbyRef.childByAppendingPath(self.userRef.authData.uid)
standbyUserRef.updateChildValues(standbyData)
});
})
});

关于ios - 同步调度不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28530124/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com