- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个函数要用 Swift (X-Code) 制作。我基本上是通过蓝牙发送一些命令,我对 Main Asyncafter 的工作原理有疑问。这是我的设置代码:
func tPodSetUp()
{
var delayForResponse = DispatchTime.now() + 0.4 //seconds to wait for response
writeValue(data: "231") //Put t-Pod in command mode, burst mode is OFF returns OK
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if receivedString1.lowercased() == "ok"
{
print("t-Pod burst mode is OFF")
}
}
writeValue(data: "202") //load calibration constants of probe, returns ok or 0
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if receivedString1.lowercased() == "ok"
{
print("calibration Loaded")
}
if receivedString1 == "0"
{
print("No probe connected")
}
}
}
我希望它基本上执行以下操作(按此顺序):
写值
等待 .4 秒
读取响应/检查响应
writeValue(在它完成读取/检查响应之后)
读取响应/检查响应
我担心,如果代码是现在这样,它会在等待其他值时超过 writeValues,并将它们放在异步运行的单独线程上。
另外,让我感到困惑的是,我在一开始就声明了 delayForResponse,并说每次调用时都会改变吗?就像如果我在 12:00:00:00 执行它,它现在将完成 + .4 秒(所以它应该在 12:00:00:40 被调用,但是在 12:00:00 会发生什么: 41 当它运行第二部分时(有另一个对 delayForResponse 的标注)它会突然说“什么?这本应该在 .01 秒前完成的!?
编辑 这是根据一些反馈对代码进行的另一种处理,这会按照我的想法进行吗?
let setupQueue = DispatchQueue(label: "setupQueue")
let delayForResponse = DispatchTime.now() + 0.4 //seconds to wait for response
setupQueue.sync {
writeValue(data: String(UnicodeScalar(UInt8(231)))) //231: Put t-Pod in command mode, burst mode is OFF returns OK
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if receivedString1.lowercased() == "ok"
{
print("t-Pod burst mode is OFF")
}
}
writeValue(data: String(UnicodeScalar(UInt8(202)))) //202: load calibration constants of probe, returns ok or 0
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if receivedString1.lowercased() == "ok"
{
print("t-Pod burst mode is OFF")
}
if receivedString1 == "0"
{
print("No probe connected")
}
}
writeValue(data: String(UnicodeScalar(UInt8(204)))) //204: load t-Pod serial number
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if (receivedString1.count > 5)
{
print("received t-Pod SN: \(receivedString1)")
tPodSN = receivedString1
}
}
writeValue(data: String(UnicodeScalar(UInt8(205)))) //205: load probe serial number
DispatchQueue.main.asyncAfter(deadline: delayForResponse)
{
if (receivedString1.count > 3)
{
print("received Probe SN: \(receivedString1)")
probeSN = receivedString1
}
}
//AFTER FINISHING SETUP, RESET TPOD AND TURN BEACON OFF
writeValue(data: String(UnicodeScalar(UInt8(202)))) //200: resets t-Pod
writeValue(data: String(UnicodeScalar(UInt8(202)))) //211: turns beacon off (temperature output)
}
最佳答案
你检查过DispatchGroup
了吗??
func myFunction() {
var a: Int?
let group = DispatchGroup()
group.enter()
DispatchQueue.main.async {
a = 1
group.leave()
}
// does not wait. But the code in notify() is run
// after enter() and leave() calls are balanced
group.notify(queue: .main) {
print(a)
}
}
关于swift - DispatchQueue会互相等待吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46434491/
dispatch_apply将调度队列作为参数,它允许您选择要在哪个队列上执行 block 。 我的理解是,Swift中的DispatchQueue.concurrentPerform是用来代替dis
我试图了解 Dispatch Sync 和 Dispatch Async,我知道它以 GCD 的同步和异步方式执行。但是当我尝试下面的代码时,它给了我奇怪的场景。 我在 Playground 和同步块
当我在主线程上并尝试 DispatchQueue.main.async 时,它不会使应用程序崩溃,但 DispatchQueue.main.sync 会。为什么会这样? 最佳答案 根据 Docs sy
我一直在用DispatchQueue.main.async长时间执行UI相关操作。 Swift 同时提供 DispatchQueue.main.async和 DispatchQueue.main.sy
我正在阅读教程: https://www.raywenderlich.com/148513/grand-central-dispatch-tutorial-swift-3-part-1 并且遇到了 Q
我试图了解 DispatchQueues 的真正工作原理,我想知道假设 DispatchQueues 有自己的管理线程是否安全?例如,让我们以串行队列为例。在我的理解中——因为它是串行的,新任务只能在
So my goal here is to basically perform a query in a while loop and append results of the query to m
我有一个简单的应用程序,其工作原理如下: 应用加载时使用默认图像 收到点击后,将从列表中加载随机图像 如果 5 秒内没有收到点击,我们将重置回默认图像 我正在尝试使用 DispatchQueue 和
我想为不同的 http 请求设置不同的优先级,并且我希望能够立即停止/暂停一些请求,并在我有一个 High 的请求时只执行其中的一些请求 优先级。我打算使用不同的方法,但现在似乎所有方法都已弃用: 1
我正在异步创建图像,然后在主线程上更新 UI。例如 imageLoadingQueue.async { // Creates a new image, caches it. let n
试图制作线程安全数组,但它不像我预期的那样工作 import Foundation import PlaygroundSupport PlaygroundPage.current.needsIndef
我需要在 Swift 中执行搜索操作,我需要使用 UISearchbar。 在 textDidChange 事件中,我需要调用 Web API,解析响应,然后更新数组,然后开始搜索更新后的数组。 但不
我对 DispatchQueue 东西(包括 DispatchGroups、Qos 等)不是很熟悉。我现在有一个担忧。就像,我有一个 ViewController 被重用于 Android 的 Vie
我正在尝试延迟循环某些内容(只是作为概念证明)这是为了其他目的。但为了简化它,这里有一个例子。 所以在这个例子中,我有一个带有“文本”的字符串,我想循环添加另一个字符串,比方说 10 次。唯一的问题是
我正在尝试创建一个使用 USPS API 获取数据的简单包裹跟踪应用程序。 callRestService 方法成功获取数据,其完成处理程序 serviceCallback(设置 unparsedXm
好吧,我已经经历了大量的问题和答案,我知道对它的理解,但是当我尝试一些代码时,我得到了一些不符合这些理论的结果。 目前我的理解: Sync:一旦 block 内的所有任务都被执行,控制将返回。 异步:
我正在为一个应用程序项目编写一个简单的缓存框架,并在闭包中使用 GCD 和 DispatchQueues 来保存和检索框架中的对象。 在编程时,我一次又一次地质疑自己是否正确地完成了它。这是我正在做的
我有这段代码: DispatchQueue.main.asyncAfter(deadline: .now() + (delay * Double(isDelayAccounted.hashVa
这是一个奇怪的问题,我似乎找不到任何帮助。在 Swift 3 上,尝试调用主线程我尝试使用 DispatchQueue.main.async,但它无法编译。 任何想法都会有所帮助。 最佳答案 试试这段
在 Swift 3 中,创建 DispatchQueue 实例: DispatchQueue(label: String, qos: DispatchQoS,
我是一名优秀的程序员,十分优秀!