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)
           }
        }
    

    相关文章

      网友评论

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

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