为什么我无法从 Alamofire 获取请求结果
Why can't I get the request result form Alamofire
我无法获得 Alamofire 的结果 request.So,我创建了我从 json 异步获得的数组的输出 call.I 无法从调度中获得 resultArray {...}。当我添加 println 进行调试时,code.The 第二个先出现在第一个之前。
我只想通过 resultArray 从 Alamofire 获取数据以显示在 UIPickerView.Please Help!!!
这是我的代码
import UIKit
import Alamofire
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
@IBOutlet var venuePicker : UIPickerView?
var resultOld = [String:String]() // i need it to get only value from json
var result : [String]?
let refreshControl = UIRefreshControl()
override func viewDidLoad() {
if result == nil {
populateVenues ({ (error, result) -> Void in
self.result = result as? [String]
self.venuePicker?.reloadAllComponents()
})
}
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if result != nil{
return result!.count// Why i can't use result?.count instead of result!.count
}
else{
return 0
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if result != nil{
println(result)
return result?[row]
}
else{
return "..."
}
}
func populateVenues(completion : (error: NSError?,result : AnyObject?) -> Void){
Alamofire.request(.POST, "http://xxxx.xxxx.xxx").responseJSON() {
(_, _, jsonData, error) in
if error == nil{
var venues = JSON(jsonData!)
for (k, v) in venues {
self.resultOld[k] = v.arrayValue[0].stringValue
}
self.result = self.resultOld.values.array
completion(error: nil,result: self.result)
}
else{
println("Error!!")
completion(error: error!,result: nil)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是我在控制台的输出
The 2st result array : []
The 1st result array : [ORIX Kobe Nyusatsu, NPS Sendai Nyusatsu, JU Gunma, KAA Kyoto, JU Ibaraki, USS Gunma, ISUZU Kobe, NAA Osaka Nyusatsu, SMAP Sapporo Nyusatsu, L-Up PKobeNyusatsu, ARAI Sendai, TAA Minami Kyushu, NPS Oyama Nyusatsu, CAA Tokyo, JU Toyama, USS Shikoku, NPS Gifu Nyusatsu, NAA Fukuoka, KCAA Yamaguchi, JU Fukuoka, LAA Kansai, JAA, TAA Kinki, USS Sapporo, JU Miyagi, USS Fukuoka, JU Tokyo]
拜托,我真的需要知道发生了什么事以及为什么我不能先得到异步调用的结果。
所以异步调用是在另一个线程上执行的。因此,当您调用函数 populateVenue()
时,populateVenue()
函数未在 println("The 2st result array : \(self.resultArray)" )
之前完成。如果您将 populateVenue()
设置为关闭,则不会发生这种情况。
示例:
override func viewDidLoad() {
super.viewDidLoad()
populateVenue( { (error, result) -> Void in
println("The 2st result array : \(self.resultArray)" )
})
}
func populateVenue(completion: (error: NSError?, result: AnyObject?) -> Void) {
Alamofire.request(.POST, "http://localhost:8080/ws/automobile/global/auction/latest/venues").responseJSON() {
(_, _, jsonData, error) in
if error == nil {
// do whatever you need
// Note that result is whatever data you retrieved
completion(nil, result)
} else {
println("Errror")
completion(error!, nil)
}
}
}
编辑:
我仍在努力理解您的问题,但这是我最好的选择。请注意,我不知道 resultOld 的用途是什么,所以我删除了它。如果你绝对需要它,你可以将它添加回去。我在这里的设计是使属性可选,并且 return 完成块中的结果。然后在 viewDidLoad 中,您可以初始化 属性 数组并重新加载屏幕。
@IBOutlet var venuePicker : UIPickerView?
// Try making this optional so you can tell when the network call is completed
var result: [String]?
var error = "Error"
let refreshControl = UIRefreshControl()
override func viewDidLoad() {
if result == nil {
populateVenues ( { (result) -> Void in
self.result = result
self.venuePicker?.reloadAllComponents()
})
}
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if result != nil {
return result.count
} else {
return 0
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return result[row]
}
func populateVenues(completion : (result : [String]?) -> Void){
Alamofire.request(.POST, "http://localhost:8080/ws/automobile/global/auction/latest/venues").responseJSON() {
(_, _, jsonData, error) in
if error == nil{
var venues = JSON(jsonData!)
for (k, v) in venues {
resultOld[k] = v.arrayValue[0].stringValue
}
result = resultOld.values.array
completion(result: result)
}
else{
println("Error!!")
completion(result: nil)
}
}
}
我无法获得 Alamofire 的结果 request.So,我创建了我从 json 异步获得的数组的输出 call.I 无法从调度中获得 resultArray {...}。当我添加 println 进行调试时,code.The 第二个先出现在第一个之前。 我只想通过 resultArray 从 Alamofire 获取数据以显示在 UIPickerView.Please Help!!!
这是我的代码
import UIKit
import Alamofire
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
@IBOutlet var venuePicker : UIPickerView?
var resultOld = [String:String]() // i need it to get only value from json
var result : [String]?
let refreshControl = UIRefreshControl()
override func viewDidLoad() {
if result == nil {
populateVenues ({ (error, result) -> Void in
self.result = result as? [String]
self.venuePicker?.reloadAllComponents()
})
}
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if result != nil{
return result!.count// Why i can't use result?.count instead of result!.count
}
else{
return 0
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if result != nil{
println(result)
return result?[row]
}
else{
return "..."
}
}
func populateVenues(completion : (error: NSError?,result : AnyObject?) -> Void){
Alamofire.request(.POST, "http://xxxx.xxxx.xxx").responseJSON() {
(_, _, jsonData, error) in
if error == nil{
var venues = JSON(jsonData!)
for (k, v) in venues {
self.resultOld[k] = v.arrayValue[0].stringValue
}
self.result = self.resultOld.values.array
completion(error: nil,result: self.result)
}
else{
println("Error!!")
completion(error: error!,result: nil)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是我在控制台的输出
The 2st result array : []
The 1st result array : [ORIX Kobe Nyusatsu, NPS Sendai Nyusatsu, JU Gunma, KAA Kyoto, JU Ibaraki, USS Gunma, ISUZU Kobe, NAA Osaka Nyusatsu, SMAP Sapporo Nyusatsu, L-Up PKobeNyusatsu, ARAI Sendai, TAA Minami Kyushu, NPS Oyama Nyusatsu, CAA Tokyo, JU Toyama, USS Shikoku, NPS Gifu Nyusatsu, NAA Fukuoka, KCAA Yamaguchi, JU Fukuoka, LAA Kansai, JAA, TAA Kinki, USS Sapporo, JU Miyagi, USS Fukuoka, JU Tokyo]
拜托,我真的需要知道发生了什么事以及为什么我不能先得到异步调用的结果。
所以异步调用是在另一个线程上执行的。因此,当您调用函数 populateVenue()
时,populateVenue()
函数未在 println("The 2st result array : \(self.resultArray)" )
之前完成。如果您将 populateVenue()
设置为关闭,则不会发生这种情况。
示例:
override func viewDidLoad() {
super.viewDidLoad()
populateVenue( { (error, result) -> Void in
println("The 2st result array : \(self.resultArray)" )
})
}
func populateVenue(completion: (error: NSError?, result: AnyObject?) -> Void) {
Alamofire.request(.POST, "http://localhost:8080/ws/automobile/global/auction/latest/venues").responseJSON() {
(_, _, jsonData, error) in
if error == nil {
// do whatever you need
// Note that result is whatever data you retrieved
completion(nil, result)
} else {
println("Errror")
completion(error!, nil)
}
}
}
编辑:
我仍在努力理解您的问题,但这是我最好的选择。请注意,我不知道 resultOld 的用途是什么,所以我删除了它。如果你绝对需要它,你可以将它添加回去。我在这里的设计是使属性可选,并且 return 完成块中的结果。然后在 viewDidLoad 中,您可以初始化 属性 数组并重新加载屏幕。
@IBOutlet var venuePicker : UIPickerView?
// Try making this optional so you can tell when the network call is completed
var result: [String]?
var error = "Error"
let refreshControl = UIRefreshControl()
override func viewDidLoad() {
if result == nil {
populateVenues ( { (result) -> Void in
self.result = result
self.venuePicker?.reloadAllComponents()
})
}
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if result != nil {
return result.count
} else {
return 0
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return result[row]
}
func populateVenues(completion : (result : [String]?) -> Void){
Alamofire.request(.POST, "http://localhost:8080/ws/automobile/global/auction/latest/venues").responseJSON() {
(_, _, jsonData, error) in
if error == nil{
var venues = JSON(jsonData!)
for (k, v) in venues {
resultOld[k] = v.arrayValue[0].stringValue
}
result = resultOld.values.array
completion(result: result)
}
else{
println("Error!!")
completion(result: nil)
}
}
}