在Swift中链接多个异步函数 [英] Chaining multiple async functions in Swift
问题描述
我想写一系列函数来验证用户的信息,然后再请求他们确认。 (想象购物应用程式)。
- 我必须先检查使用者是否已新增卡片。
- 然后,我必须检查他们是否有足够的余额。
- 然后,我可以要求他们确认付款。
我可以写异步方法来检查卡的样子...
func checkHasCard完成:(Bool) - >()){
//转到互联网
//获取卡
//进程数据
让hasCard:Bool = /用户是否有卡。
完成(hasCard)
}
checkHasCard {
hasCard in
if hasCard {
print(YAY! )
} else {
print(BOO!)
}
}
$ b b
但是...现在,基于我必须做各种事情。如果用户有卡,我然后需要继续向前,并检查是否有足够的余额(以大致相同的方式)。如果用户没有卡,我会为他们添加他们的卡的屏幕。
但它变得凌乱...
<$ c $ p>
checkHasCard {
hasCard in
if hasCard {
// check balance
print(YAY! b $ b checkBalance {
hasBalance in
if hasBalance {
// WHAT IS GOING ON ?!
print()
} else {
//请求充值帐户
print(BOO!)
}
}
} else {
//请求卡详细信息
print(BOO!)
}
}
pre> 我想要的是沿着这条线的东西。
checkHasCard()//如果没有卡然后显示卡详细信息屏幕
.checkBalance()//只有在有卡时运行...如果没有余额请求充值
.confirmPayment()
这看起来更像swifty,但我不知道如何更近
有办法吗?
异步操作,有序和依赖?
你当然可以使用GCD连接任务:
DispatchQueue.main.async {
//做某事
//检查某事...
//然后:
DispatchQueue.main.async {
//从更高的闭包接收信息
//等等
}
}
b $ b
但是如果你的操作很复杂,例如他们有代表,那个架构完全崩溃了。 NSOperation允许复杂的操作以你以后的方式封装。
I'm trying to write a series of functions that will validate the user's information before asking them to confirm something. (Imagine a shopping app).
- I first have to check that the user has added a card.
- Then I have to check that they have sufficient balance.
- Then I can ask them to confirm the payment.
I can write the async method to check the card something like ...
func checkHasCard(completion: (Bool) -> ()) {
// go to the inter webs
// get the card
// process data
let hasCard: Bool = // the user has a card or not.
completion(hasCard)
}
This can be run like this...
checkHasCard {
hasCard in
if hasCard {
print("YAY!")
} else {
print("BOO!")
}
}
But... now, based off that I have to do various things. If the user does have a card I then need to continue onwards and check there is sufficient balance (in much the same way). If the user does not have a card I present a screen for them to add their card.
But it gets messy...
checkHasCard {
hasCard in
if hasCard {
// check balance
print("YAY!")
checkBalance {
hasBalance in
if hasBalance {
// WHAT IS GOING ON?!
print("")
} else {
// ask to top up the account
print("BOO!")
}
}
} else {
// ask for card details
print("BOO!")
}
}
What I'd like instead is something along the lines of this...
checkHasCard() // if no card then show card details screen
.checkBalance() // only run if there is a card ... if no balance ask for top up
.confirmPayment()
This looks much more "swifty" but I'm not sure how to get closer to something like this.
Is there a way?
Asynchronous operations, ordered and with dependencies? You're describing NSOperation.
Certainly you can chain tasks using GCD:
DispatchQueue.main.async {
// do something
// check something...
// and then:
DispatchQueue.main.async {
// receive info from higher closure
// and so on
}
}
But if your operations are complex, e.g. they have delegates, that architecture completely breaks down. NSOperation allows complex operations to be encapsulated in just the way you're after.
这篇关于在Swift中链接多个异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!