Swift 用 return 值编写一个 async/await 方法
Swift write an async/await method with return value
我想编写一个具有 return 值的异步等待方法,但我的代码不起作用。我还尝试了另一种方式,例如 DispatchQueue.global
DispatchGroup()
等等。
这是我的代码:
func checkPassCode() -> Bool {
var result = false
let closure = { (_ flag:Bool) -> Void in
result = flag
}
if var pin = self.keychain.get("pin") {
let userPin = self.pin.joined(separator: "")
let encryptedData = NSData(base64Encoded: pin, options: [])
AsymmetricCryptoManager.sharedInstance.decryptMessageWithPrivateKey(encryptedData! as Data) { (success, result, error) -> Void in
if success {
pin = result!
print("userPin is: \(userPin)")
print("storePin is: \(pin)")
closure(userPin == pin)
} else {
print("Error decoding base64 string: \(String(describing: error))")
closure(false)
}
}
}
return result
}
谢谢,vadian 评论。我使用闭包作为方法的输入参数。
// MARK: - PassCode Methods
func checkPassCode(completionHandler:@escaping (_ flag:Bool) -> ()) {
let storePin = getStorePin()
let userPin = self.pin.joined(separator: "")
AsymmetricCryptoManager.sharedInstance.decryptMessageWithPrivateKey(storePin as Data) { (success, result, error) -> Void in
if success {
let pin = result!
print("userPin is: \(userPin)")
print("storePin is: \(pin)")
completionHandler(userPin == pin)
} else {
print("Error decoding base64 string: \(String(describing: error))")
completionHandler(false)
}
}
}
func getStorePin() -> NSData {
if let pin = self.keychain.get("pin") {
return NSData(base64Encoded: pin, options: []) ?? NSData()
}
return NSData()
}
然后调用这个方法:
checkPassCode { success in
if success {
print("sucess")
} else {
print("not sucess!")
}
}
您可以将此框架用于 Swift 协程 - https://github.com/belozierov/SwiftCoroutine
当你调用 await 时,它不会阻塞线程,只会挂起协程,所以你也可以在主线程中使用它。
func awaitCheckPassCode() throws -> Bool {
let storePin = getStorePin()
let userPin = self.pin.joined(separator: "")
let manager = AsymmetricCryptoManager.sharedInstance
let (success, result, _) = try Coroutine.await {
manager.decryptMessageWithPrivateKey(storePin as Data, completion: [=10=])
}
return success && userPin == result
}
然后在协程中调用这个方法:
DispatchQueue.main.startCoroutine {
let success = try awaitCheckPassCode()
if success {
print("sucess")
} else {
print("not sucess!")
}
}
我想编写一个具有 return 值的异步等待方法,但我的代码不起作用。我还尝试了另一种方式,例如 DispatchQueue.global
DispatchGroup()
等等。
这是我的代码:
func checkPassCode() -> Bool {
var result = false
let closure = { (_ flag:Bool) -> Void in
result = flag
}
if var pin = self.keychain.get("pin") {
let userPin = self.pin.joined(separator: "")
let encryptedData = NSData(base64Encoded: pin, options: [])
AsymmetricCryptoManager.sharedInstance.decryptMessageWithPrivateKey(encryptedData! as Data) { (success, result, error) -> Void in
if success {
pin = result!
print("userPin is: \(userPin)")
print("storePin is: \(pin)")
closure(userPin == pin)
} else {
print("Error decoding base64 string: \(String(describing: error))")
closure(false)
}
}
}
return result
}
谢谢,vadian 评论。我使用闭包作为方法的输入参数。
// MARK: - PassCode Methods
func checkPassCode(completionHandler:@escaping (_ flag:Bool) -> ()) {
let storePin = getStorePin()
let userPin = self.pin.joined(separator: "")
AsymmetricCryptoManager.sharedInstance.decryptMessageWithPrivateKey(storePin as Data) { (success, result, error) -> Void in
if success {
let pin = result!
print("userPin is: \(userPin)")
print("storePin is: \(pin)")
completionHandler(userPin == pin)
} else {
print("Error decoding base64 string: \(String(describing: error))")
completionHandler(false)
}
}
}
func getStorePin() -> NSData {
if let pin = self.keychain.get("pin") {
return NSData(base64Encoded: pin, options: []) ?? NSData()
}
return NSData()
}
然后调用这个方法:
checkPassCode { success in
if success {
print("sucess")
} else {
print("not sucess!")
}
}
您可以将此框架用于 Swift 协程 - https://github.com/belozierov/SwiftCoroutine
当你调用 await 时,它不会阻塞线程,只会挂起协程,所以你也可以在主线程中使用它。
func awaitCheckPassCode() throws -> Bool {
let storePin = getStorePin()
let userPin = self.pin.joined(separator: "")
let manager = AsymmetricCryptoManager.sharedInstance
let (success, result, _) = try Coroutine.await {
manager.decryptMessageWithPrivateKey(storePin as Data, completion: [=10=])
}
return success && userPin == result
}
然后在协程中调用这个方法:
DispatchQueue.main.startCoroutine {
let success = try awaitCheckPassCode()
if success {
print("sucess")
} else {
print("not sucess!")
}
}