我正在玩Swift中的闭包和链接完成.我非常熟悉异步的C#样式并等待,所以我想知道如何将以下代码片段从C#“转换”为Swift.
public async Task SomeFunction(string inputString)
{
var first = await GetFirstVariableAsync(inputString);
var second = await GetSecondVariableAsync(first);
if (second == "some condition")
{
first = await GetFirstVariableAsync(inputString);
var second = await GetSecondVariableAsync(first);
}
}
Swift是否有类似await的类似构造,等待函数完成,而不必嵌套多个完成块?
谢谢
解决方法:
如果我没有误会,你在寻找的是:
串行调度队列:
Serial queues (also known as private dispatch queues) execute one task
at a time in the order in which they are added to the queue. The
currently executing task runs on a distinct thread (which can vary
from task to task) that is managed by the dispatch queue. Serial
queues are often used to synchronize access to a specific resource.
You can create as many serial queues as you need, and each queue
operates concurrently with respect to all other queues. In other
words, if you create four serial queues, each queue executes only one
task at a time but up to four tasks could still execute concurrently,
one from each queue. For information on how to create serial queues,
see Creating Serial Dispatch Queues.
斯威夫特3:
let serialQueue = DispatchQueue(label: "serialQueue")
serialQueue.sync {
print("running first task")
}
serialQueue.sync {
print("I will wait the first task and then I'll do my own task")
}
原文地址:https://codeday.me/bug/20190627/1309058.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。