如何使用Alamofire发出同步请求? [英] How to make a synchronous request using Alamofire?

查看:332
本文介绍了如何使用Alamofire发出同步请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Alamofire 进行同步请求。我查看了Stackoverflow,并发现了以下问题:使异步alamofire请求同步

I am trying to do a synchronous request using Alamofire. I have looked on Stackoverflow and found this question: making an asynchronous alamofire request synchronous.

我看到接受的答案使用完成来制作 Alamofire 请求同步,但是我无法使其工作。这是我的简化代码:

I saw that the accepted answer uses completion to make Alamofire request synchronous but I cannot make it to work. This is my simplified code:

func loadData(completion: (Bool)) -> (Int, [String], [String], [String]){

    Alamofire.request(url!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { response in

        switch(response.result) {
        case .success(_):
            if let JSON = response.result.value as! [[String : AnyObject]]!{
                 //Here I retrieve the data
            }

            completion(true)
            break

        case .failure(_):
            print("Error")
            completion(false)
            break  
        }
   }

   return (numberRows, nameArray, ageArray, birthdayArray)
}

使用此代码,我得到一个错误尝试使完成(布尔值)时。我得到的错误如下:

With this code I am getting an error when trying to make completion(bool value). The error that I am getting is the following:


无法调用非函数类型'Bool'的值

Cannot call value of non-function type 'Bool'

我尝试使用很多使用完成的示例来同步获取值(因为我需要先检索数据才能将其显示在表上并同时

I have tried using a lot of examples using completion to get the values synchronously (because I need to retrieve the data before to show it on a table and at the same time get the number of rows of that table) without success.

如何使用该完成来获取同步响应?

How can I use that completion to get a synchronous response?

预先感谢!

推荐答案

在使用完成处理程序时不要使用return。

when you use completion handler do not use return.

func loadData(completion: @escaping (_ number: Int, _ strArr1: [String], _ strArr2: [String], _ strArr3: [String]) -> ()){

  Alamofire.request(url!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { response in

    switch(response.result) {
    case .success(_):
        if let JSON = response.result.value as! [[String : AnyObject]]!{
            //Here I retrieve the data
        }
        completion(number: numberRows, strArr1 : nameArray, strArr2 : ageArray, strArr3: birthdayArray)
        break

    case .failure(_):
        print("Error")
        completion(number: numberRows, strArr1 : nameArray, strArr2 : ageArray, strArr3: birthdayArray)
        break
    }
  }
}

loadData (completion: { (number, strArr1, strArr2, strArr3) in
    // do it
    // for exapmple
    self.number = number
    self.strArr1 = strArr1
    // and so on

})

或者,如果要在闭包中返回任何值,则必须使用完成处理程序返回任何值或类似的东西,例如if您要返回布尔值:

or if you want return any value in closure you must use completion handler for return any value or some thing like, for example if you want return Boolean value:

func loadData(completion:(number: numberRows, strArr1 : nameArray, strArr2 : ageArray, strArr3: birthdayArray) -> (Bool))

并在 loadData

loadData( completion: { ( number, strArr1, strArr2, strArr3 ) -> (Bool) in
       # code 
       return False
})

或其他人。

我使用swift3。但是,如果要使用另一个版本的swift,请注意外部参数名称和内部参数名称,例如: @转义(_数字:Int,_ strArr1:[String],_ strArr2:[String],_ strArr3:[String])-> ())

I use swift 3. but if you want another version of swift careful about External Parameter Names and internal parameter names, like: @escaping (_ number: Int, _ strArr1: [String], _ strArr2: [String], _ strArr3: [String]) -> ())

如果要设置外部参数名称,只需删除 _ 并设置参数名称。

if you want set external parameter names, just need drop _ and set name for parameters.

这篇关于如何使用Alamofire发出同步请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆