swift 异步 async/await 的使用

作者: Mehmet | 来源:发表于2021-11-29 15:13 被阅读0次

在5.5之前进行异步操作,调用返回时,使用completion handler参数进行处理。现在提供了async/awiat 进行异步并发处理。

基本使用方式:

func sendCode() async throws -> Data{

    let request = URLRequest.init(url: URL(string: "")!)
    let (data, response) = try await URLSession.shared.data(for: request)
    
    guard (response as? HTTPURLResponse)?.statusCode == 200 else {
        throw RequestError.notdata
    }
    
    return data      
}

方法后面跟上async 表示是一个异步函数。如果调用异步函数

func groupTaskLoadData() async throws {   
    do{
        try await sendCode()
    }catch let error as RequestError{
       if error == RequestError.urlerror {
           print("xxxx")
        }
    }       
}

在正常返回的函数中使用 async 修饰的func时,需要用Task{} 进行包装,否则报错

Cannot pass function of type '() async -> Void' to parameter expecting synchronous function type

使用方式:

Button("Test Async") {
     
  Task{ 
     await sendCode()   
    }
}

属性也可以 async properties

var isAuth: Bool {
    get async {
        await self.sendCode()
    }
}

使用异步属性,必须只能是get属性。可写属性不能使用异步属性。


Continuation 模式

如果之前使用的completion handler方式的方法,或者第三方的库中使用的completion 方式。需要进行包装后使用。swift 提供了 withUnsafeContinuationwithCheckedThrowingContinuationwithCheckedContinuation 函数。

public func resume(returning x: T) 接收 completion 中的数据返回,转换成async 函数返回。

public func resume(throwing x: E) 进行抛出异常
withCheckedContinuation 方法中的checked 会在运行时对操作进行检查:是否调用resume进行返回。如果不调用会造成资源泄露。多次调用也会造成问题。
continuation 有且只能 resume 一次。

withUnsafeContinuation 的工作机制和withCheckedContinuation 一致,唯一区别在于运行时不会对操作进行检查。但性能更好。实际使用中withCheckedContinuation测试没有问题后,正式发布时使用withUnsafeContinuation

使用方式如下:

withCheckedContinuation

func sendCodeContinuation() async -> Data{
    
    await withCheckedContinuation{ continuation in
        NET.GET(url: "").success { data in
            continuation.resume(returning: data)
        }
    } as! Data
}

如果有抛出异常
withCheckedThrowingContinuation

func sendCodeThrowsContinuation() async throws -> Data{
    
    try await withCheckedThrowingContinuation { (continuation:CheckedContinuation<Data,Error>) in
       NET.POST(url: "").success { data in
           continuation.resume(returning: data as! Data)

       }.failed { error in
           continuation.resume(throwing: error as! Error)
       }
    }

相关文章

  • async和await

    浅谈Async/Await用 async/await 来处理异步 async和await async:声明一个异步...

  • 关于回调的终极解决方案

    使用async和await把异步变成同步

  • dart(五)--异步、生成器

    异步 async,await 可以使同步代码异步执行。只有在async的函数里可以使用await。该函数执行到aw...

  • asp.net core 中async/await 使用

    asp.net core 中async/await 使用 async/await是用来进行异步调用的形式,内部其实...

  • flutter中compute和isolate

    async和await:对于普通的任务,使用async和await可实现异步处理任务,而async的处理方式并非使...

  • Dart 同步、异步、队列

    异步 Dart中的异步看起来像是同步 dart中使用 async方法和await 表达式实现异步 要使用await...

  • async

    async/await特点 async/await更加语义化,async是“异步”的简写,async functi...

  • swift 异步 async/await 的使用

    在5.5之前进行异步操作,调用返回时,使用completion handler参数进行处理。现在提供了async/...

  • ES8(一) —— async&await

    目录 async和普通函数的区别 await async/await处理多回调异步 async和await必须配合...

  • Swift5.5 使用async await

    新旧方法对比 Swift 5.5添加了async await 用于支持异步编程,使用起来确实比之前方便多了。新的方...

网友评论

    本文标题:swift 异步 async/await 的使用

    本文链接:https://www.haomeiwen.com/subject/xtllxrtx.html