从Swift函数中的异步调用返回数据

时间:2014-08-08 12:28:59

标签: ios rest asynchronous swift

我在Swift项目中创建了一个实用程序类,它处理所有REST请求和响应。我已经构建了一个简单的REST API,因此我可以测试我的代码。我创建了一个需要返回NSArray的类方法,但由于API调用是异步的,我需要从异步调用中的方法返回。问题是异步返回void。 如果我在Node中这样做,我将使用JS承诺,但我无法找到适用于Swift的解决方案。

import Foundation

class Bookshop {
    class func getGenres() -> NSArray {
        println("Hello inside getGenres")
        let urlPath = "http://creative.coventry.ac.uk/~bookshop/v1.1/index.php/genre/list"
        println(urlPath)
        let url: NSURL = NSURL(string: urlPath)
        let session = NSURLSession.sharedSession()
        var resultsArray:NSArray!
        let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
            println("Task completed")
            if(error) {
                println(error.localizedDescription)
            }
            var err: NSError?
            var options:NSJSONReadingOptions = NSJSONReadingOptions.MutableContainers
            var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: options, error: &err) as NSDictionary
            if(err != nil) {
                println("JSON Error \(err!.localizedDescription)")
            }
            //NSLog("jsonResults %@", jsonResult)
            let results: NSArray = jsonResult["genres"] as NSArray
            NSLog("jsonResults %@", results)
            resultsArray = results
            return resultsArray // error [anyObject] is not a subType of 'Void'
        })
        task.resume()
        //return "Hello World!"
        // I want to return the NSArray...
    }
}

14 个答案:

答案 0 :(得分:76)

您可以传递回调,并在异步调用中调用回调

类似的东西:

class func getGenres(completionHandler: (genres: NSArray) -> ()) {
    ...
    let task = session.dataTaskWithURL(url) {
        data, response, error in
        ...
        resultsArray = results
        completionHandler(genres: resultsArray)
    }
    ...
    task.resume()
}

然后调用此方法:

override func viewDidLoad() {
    Bookshop.getGenres {
        genres in
        println("View Controller: \(genres)")     
    }
}

答案 1 :(得分:11)

Swiftz已经提供了Future,这是Promise的基本构建块。未来是一个不能失败的承诺(这里的所有术语都基于Scala解释,where a Promise is a Monad)。

https://github.com/maxpow4h/swiftz/blob/master/swiftz/Future.swift

希望最终会扩展到一个完整的Scala风格的Promise(我可能会在某个时候自己写出来;我相信其他的PR会受到欢迎;对于Future已经到位并不难了。)

在您的特定情况下,我可能会创建一个Result<[Book]>(基于Alexandros Salazar's version of Result)。然后您的方法签名将是:

class func fetchGenres() -> Future<Result<[Book]>> {

注释

  • 我不建议在Swift中使用get前缀函数。它将破坏与ObjC的某些互操作性。
  • 我建议您将结果一直解析为Book对象,然后将结果作为Future返回。这个系统可以通过多种方式发生故障,如果在将它们包装到Future之前检查所有这些内容,会更方便。对于其他Swift代码而言,访问[Book]比处理NSArray要好得多。

答案 2 :(得分:4)

Swift 4.0

对于异步请求 - 响应,您可以使用完成处理程序。见下文我已经用完成句柄范例修改了解决方案。

func getGenres(_ completion: @escaping (NSArray) -> ()) {

        let urlPath = "http://creative.coventry.ac.uk/~bookshop/v1.1/index.php/genre/list"
        print(urlPath)

        guard let url = URL(string: urlPath) else { return }

        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            guard let data = data else { return }
            do {
                if let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary {
                    let results = jsonResult["genres"] as! NSArray
                    print(results)
                    completion(results)
                }
            } catch {
                //Catch Error here...
            }
        }
        task.resume()
    }

您可以按以下方式调用此功能:

getGenres { (array) in
    // Do operation with array
}

答案 3 :(得分:3)

Swift 3版@Alexey Globchastyy的回答:

class func getGenres(completionHandler: @escaping (genres: NSArray) -> ()) {
...
let task = session.dataTask(with:url) {
    data, response, error in
    ...
    resultsArray = results
    completionHandler(genres: resultsArray)
}
...
task.resume()
}

答案 4 :(得分:3)

我希望您仍然不会对此停留,但是简单的答案是您不能在Swift中做到这一点。

另一种方法是返回一个回调,该回调将在数据准备就绪后立即提供您所需的数据。

答案 5 :(得分:3)

基本模式是使用完成处理程序闭包。

例如,在即将推出的Swift 5中,您将使用Result

func fetchGenres(completion: @escaping (Result<[Genre], Error>) -> Void) {
    ...
    URLSession.shared.dataTask(with: request) { data, _, error in 
        if let error = error {
            DispatchQueue.main.async {
                completion(.failure(error))
            }
            return
        }

        // parse response here

        let results = ...
        DispatchQueue.main.async {
            completion(.success(results))
        }
    }.resume()
}

您会这样称呼它:

fetchGenres { results in
    switch results {
    case .success(let genres):
        // use genres here, e.g. update model and UI

    case .failure(let error):
        print(error.localizedDescription)
    }
}

// but don’t try to use genres here, as the above runs asynchronously

注意,上面我将完成处理程序分派回主队列,以简化模型和UI更新。一些开发人员对此做法表示例外,他们可以使用所使用的任何队列URLSession或使用自己的队列(要求调用者自己手动同步结果)。

但这在这里并不重要。关键问题是使用完成处理程序来指定异步请求完成后要运行的代码块。


较早的Swift 4模式是:

func fetchGenres(completion: @escaping ([Genre]?, Error?) -> Void) {
    ...
    URLSession.shared.dataTask(with: request) { data, _, error in 
        if let error = error {
            DispatchQueue.main.async {
                completion(nil, error)
            }
            return
        }

        // parse response here

        let results = ...
        DispatchQueue.main.async {
            completion(results, error)
        }
    }.resume()
}

您会这样称呼它:

fetchGenres { genres, error in
    guard let genres = genres, error == nil else {
        // handle failure to get valid response here

        return
    }

    // use genres here
}

// but don’t try to use genres here, as the above runs asynchronously

请注意,以上我已停用NSArray的使用(我们不再使用those bridged Objective-C types)。我假设我们有一个Genre类型,并且大概使用了JSONDecoder而不是JSONSerialization来对其进行解码。但是此问题没有足够的有关底层JSON的信息以在此处进行详细介绍,因此我为避免混淆核心问题,省略了使用闭包作为完成处理程序。

答案 6 :(得分:1)

斯威夫特 5.5:

TL;DR:Swift 5.5 尚未发布(在撰写本文时)。要使用 swift 5.5,请从 here 下载 swift 工具链开发快照并添加编译器标志 -Xfrontend -enable-experimental-concurrency。阅读更多here

这可以通过 async/await 功能轻松实现。

为此,您应该将您的函数标记为 async,然后在 withUnsafeThrowingContinuation 块内执行如下操作。

class Bookshop {
  class func getGenres() async throws -> NSArray {
    print("Hello inside getGenres")
    let urlPath = "http://creative.coventry.ac.uk/~bookshop/v1.1/index.php/genre/list"
    print(urlPath)
    let url = URL(string: urlPath)!
    let session = URLSession.shared
    return try await withUnsafeThrowingContinuation { continuation in
      let task = session.dataTask(with: url, completionHandler: {data, response, error -> Void in
        print("Task completed")
        if(error != nil) {
          print(error!.localizedDescription)
          continuation.resume(throwing: error!)
          return
        }
        do {
          let jsonResult = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? [String: Any]
          let results: NSArray = jsonResult!["genres"] as! NSArray
          continuation.resume(returning: results)
        } catch {
          continuation.resume(throwing: error)
        }
      })
      task.resume()
    }
  }
}

你可以像这样调用这个函数

@asyncHandler
func check() {
  do {
    let genres = try await Bookshop.getGenres()
    print("Result: \(genres)")
  } catch {
    print("Error: \(error)")
  }
}

请记住,调用 Bookshop.getGenres 方法时,调用方方法应为 async 或标记为 @asyncHandler

答案 7 :(得分:0)

主要有3种快速实现回调的方法

  1. 关闭/完成处理程序

  2. 代表

  3. 通知

异步任务完成后,也可以使用观察者来获取通知。

答案 8 :(得分:0)

创建回叫功能的方法有3种: 1.完成处理程序 2.通知 3.代表们

完成处理程序 内部的set块在源可用时执行并返回,Handler将等到响应到来之后再更新UI。

通知 一堆信息在所有应用程序上触发,Listner可以检索n个信息。通过项目获取信息的异步​​方式。

代表 调用委托时将触发一组方法,必须通过方法本身提供Source

答案 9 :(得分:0)

使用完成块,然后在主线程上激活。

主线程是UI线程,每当您执行异步任务并想要更新UI时,都必须在UI线程上进行所有UI更改

示例:

    func asycFunc(completion: () -> Void) {

                URLSession.shared.dataTask(with: request) { data, _, error in 
                    // This is an async task...!!
                    if let error = error {
                    }

                  DispatchQueue.main.async(execute: { () -> Void in
                  //When the async taks will be finished this part of code will run on the main thread
                  completion()
                })

        }

}

答案 10 :(得分:0)

有一些非常通用的要求,每个优秀的API管理器都希望满足: 将实现面向协议的API客户端。

APIClient初始接口

protocol APIClient {
   func send(_ request: APIRequest,
              completion: @escaping (APIResponse?, Error?) -> Void) 
}

protocol APIRequest: Encodable {
    var resourceName: String { get }
}

protocol APIResponse: Decodable {
}

现在请检查完整的api结构

// ******* This is API Call Class  *****
public typealias ResultCallback<Value> = (Result<Value, Error>) -> Void

/// Implementation of a generic-based  API client
public class APIClient {
    private let baseEndpointUrl = URL(string: "irl")!
    private let session = URLSession(configuration: .default)

    public init() {

    }

    /// Sends a request to servers, calling the completion method when finished
    public func send<T: APIRequest>(_ request: T, completion: @escaping ResultCallback<DataContainer<T.Response>>) {
        let endpoint = self.endpoint(for: request)

        let task = session.dataTask(with: URLRequest(url: endpoint)) { data, response, error in
            if let data = data {
                do {
                    // Decode the top level response, and look up the decoded response to see
                    // if it's a success or a failure
                    let apiResponse = try JSONDecoder().decode(APIResponse<T.Response>.self, from: data)

                    if let dataContainer = apiResponse.data {
                        completion(.success(dataContainer))
                    } else if let message = apiResponse.message {
                        completion(.failure(APIError.server(message: message)))
                    } else {
                        completion(.failure(APIError.decoding))
                    }
                } catch {
                    completion(.failure(error))
                }
            } else if let error = error {
                completion(.failure(error))
            }
        }
        task.resume()
    }

    /// Encodes a URL based on the given request
    /// Everything needed for a public request to api servers is encoded directly in this URL
    private func endpoint<T: APIRequest>(for request: T) -> URL {
        guard let baseUrl = URL(string: request.resourceName, relativeTo: baseEndpointUrl) else {
            fatalError("Bad resourceName: \(request.resourceName)")
        }

        var components = URLComponents(url: baseUrl, resolvingAgainstBaseURL: true)!

        // Common query items needed for all api requests
        let timestamp = "\(Date().timeIntervalSince1970)"
        let hash = "\(timestamp)"
        let commonQueryItems = [
            URLQueryItem(name: "ts", value: timestamp),
            URLQueryItem(name: "hash", value: hash),
            URLQueryItem(name: "apikey", value: "")
        ]

        // Custom query items needed for this specific request
        let customQueryItems: [URLQueryItem]

        do {
            customQueryItems = try URLQueryItemEncoder.encode(request)
        } catch {
            fatalError("Wrong parameters: \(error)")
        }

        components.queryItems = commonQueryItems + customQueryItems

        // Construct the final URL with all the previous data
        return components.url!
    }
}

// ******  API Request Encodable Protocol *****
public protocol APIRequest: Encodable {
    /// Response (will be wrapped with a DataContainer)
    associatedtype Response: Decodable

    /// Endpoint for this request (the last part of the URL)
    var resourceName: String { get }
}

// ****** This Results type  Data Container Struct ******
public struct DataContainer<Results: Decodable>: Decodable {
    public let offset: Int
    public let limit: Int
    public let total: Int
    public let count: Int
    public let results: Results
}
// ***** API Errro Enum ****
public enum APIError: Error {
    case encoding
    case decoding
    case server(message: String)
}


// ****** API Response Struct ******
public struct APIResponse<Response: Decodable>: Decodable {
    /// Whether it was ok or not
    public let status: String?
    /// Message that usually gives more information about some error
    public let message: String?
    /// Requested data
    public let data: DataContainer<Response>?
}

// ***** URL Query Encoder OR JSON Encoder *****
enum URLQueryItemEncoder {
    static func encode<T: Encodable>(_ encodable: T) throws -> [URLQueryItem] {
        let parametersData = try JSONEncoder().encode(encodable)
        let parameters = try JSONDecoder().decode([String: HTTPParam].self, from: parametersData)
        return parameters.map { URLQueryItem(name: $0, value: $1.description) }
    }
}

// ****** HTTP Pamater Conversion Enum *****
enum HTTPParam: CustomStringConvertible, Decodable {
    case string(String)
    case bool(Bool)
    case int(Int)
    case double(Double)

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()

        if let string = try? container.decode(String.self) {
            self = .string(string)
        } else if let bool = try? container.decode(Bool.self) {
            self = .bool(bool)
        } else if let int = try? container.decode(Int.self) {
            self = .int(int)
        } else if let double = try? container.decode(Double.self) {
            self = .double(double)
        } else {
            throw APIError.decoding
        }
    }

    var description: String {
        switch self {
        case .string(let string):
            return string
        case .bool(let bool):
            return String(describing: bool)
        case .int(let int):
            return String(describing: int)
        case .double(let double):
            return String(describing: double)
        }
    }
}

/// **** This is your API Request Endpoint  Method in Struct *****
public struct GetCharacters: APIRequest {
    public typealias Response = [MyCharacter]

    public var resourceName: String {
        return "characters"
    }

    // Parameters
    public let name: String?
    public let nameStartsWith: String?
    public let limit: Int?
    public let offset: Int?

    // Note that nil parameters will not be used
    public init(name: String? = nil,
                nameStartsWith: String? = nil,
                limit: Int? = nil,
                offset: Int? = nil) {
        self.name = name
        self.nameStartsWith = nameStartsWith
        self.limit = limit
        self.offset = offset
    }
}

// *** This is Model for Above Api endpoint method ****
public struct MyCharacter: Decodable {
    public let id: Int
    public let name: String?
    public let description: String?
}


// ***** These below line you used to call any api call in your controller or view model ****
func viewDidLoad() {
    let apiClient = APIClient()

    // A simple request with no parameters
    apiClient.send(GetCharacters()) { response in

        response.map { dataContainer in
            print(dataContainer.results)
        }
    }

}

答案 11 :(得分:0)

这是一个小用例,可能会有所帮助:-

func testUrlSession(urlStr:String, completionHandler: @escaping ((String) -> Void)) {
        let url = URL(string: urlStr)!


        let task = URLSession.shared.dataTask(with: url){(data, response, error) in
            guard let data = data else { return }
            if let strContent = String(data: data, encoding: .utf8) {
            completionHandler(strContent)
            }
        }


        task.resume()
    }

在调用函数时:-

testUrlSession(urlStr: "YOUR-URL") { (value) in
            print("Your string value ::- \(value)")
}

答案 12 :(得分:0)

Swift 5.5,基于异步/等待的解决方案

原发帖者提供的原始测试网址已失效,因此我不得不稍作更改。这个解决方案基于我找到的一个笑话 API。该 API 返回一个笑话,但我将其作为字符串数组 ([String]) 返回,以使其尽可能与原始帖子保持一致。


class Bookshop {
    class func getGenres() async -> [String] {
        print("Hello inside getGenres")
        let urlPath = "https://geek-jokes.sameerkumar.website/api?format=json"
        print(urlPath)
        let url = URL(string: urlPath)!
        let session = URLSession.shared
        
        typealias Continuation = CheckedContinuation<[String], Never>
        let genres = await withCheckedContinuation { (continuation: Continuation) in
            let task = session.dataTask(with: url) { data, response, error in
                print("Task completed")
                
                var result: [String] = []
                defer {
                    continuation.resume(returning: result)
                }
                
                if let error = error {
                    print(error.localizedDescription)
                    return
                }
                guard let data = data else { 
                    return
                }
                
                do {
                    let jsonResult = try JSONSerialization.jsonObject(with: data, options: [.mutableContainers])
                    print("jsonResult is \(jsonResult)")
                    if let joke = (jsonResult as? [String: String])?["joke"] {
                        result = [joke]
                    }
                } catch {
                    print("JSON Error \(error.localizedDescription)")
                    print("data was \(String(describing: String(data: data, encoding: .utf8)))")
                    return
                }
            }
            task.resume()
        }
        
        return genres
    }
}

async {
    let final = await Bookshop.getGenres()
    print("Final is \(final)")
}

withCheckedContinuation 是让 Swift async 函数在单独的任务/线程中实际运行的方式。

答案 13 :(得分:-1)

self.urlSession.dataTask(with: request, completionHandler: { (data, response, error) in
            self.endNetworkActivity()

            var responseError: Error? = error
            // handle http response status
            if let httpResponse = response as? HTTPURLResponse {

                if httpResponse.statusCode > 299 , httpResponse.statusCode != 422  {
                    responseError = NSError.errorForHTTPStatus(httpResponse.statusCode)
                }
            }

            var apiResponse: Response
            if let _ = responseError {
                apiResponse = Response(request, response as? HTTPURLResponse, responseError!)
                self.logError(apiResponse.error!, request: request)

                // Handle if access token is invalid
                if let nsError: NSError = responseError as NSError? , nsError.code == 401 {
                    DispatchQueue.main.async {
                        apiResponse = Response(request, response as? HTTPURLResponse, data!)
                        let message = apiResponse.message()
                        // Unautorized access
                        // User logout
                        return
                    }
                }
                else if let nsError: NSError = responseError as NSError? , nsError.code == 503 {
                    DispatchQueue.main.async {
                        apiResponse = Response(request, response as? HTTPURLResponse, data!)
                        let message = apiResponse.message()
                        // Down time
                        // Server is currently down due to some maintenance
                        return
                    }
                }

            } else {
                apiResponse = Response(request, response as? HTTPURLResponse, data!)
                self.logResponse(data!, forRequest: request)
            }

            self.removeRequestedURL(request.url!)

            DispatchQueue.main.async(execute: { () -> Void in
                completionHandler(apiResponse)
            })
        }).resume()