Matt Matt - 1 year ago 96
JSON Question

Picker view not updating

Due to some issues, I am trying to use Alamofire, here I have an Alamofire that gets an online JSON file, gets the four results, places them on the picker view, and refreshes so that they can show up. The code here works perfectly:

Alamofire.request(.GET, "", parameters: ["foo": "bar"])
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print( // server data
print(response.result) // result of response serialization

if let JSON = response.result.value {
self.mypickerview.delegate = self
self.mypickerview.dataSource = self
self.pickerData = [JSON["One"] as! String, JSON["Two"] as! String, JSON["Three"] as! String, JSON["Four"] as! String]

self.mypickerview.delegate = self;



but the problem is that with this code, while it manages to get the data successfully, it isn't showing in the picker view:

// Setup the session to make REST GET call. Notice the URL is https NOT http!!
let postEndpoint: String = ""
let session = NSURLSession.sharedSession()
let url = NSURL(string: postEndpoint)!

// Make the POST call and handle it in a completion handler
session.dataTaskWithURL(url, completionHandler: { ( data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
// Make sure we get an OK response
guard let realResponse = response as? NSHTTPURLResponse where
realResponse.statusCode == 200 else {
print("Not a 200 response")

// Read the JSON
do {
if let ipString = NSString(data:data!, encoding: NSUTF8StringEncoding) {
// Print what we got from the call

// Parse the JSON to get the data
let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
let One = jsonDictionary["One"] as! String
let Two = jsonDictionary["Two"] as! String
let Three = jsonDictionary["One"] as! String
let Four = jsonDictionary["One"] as! String

self.mypickerview.delegate = self
self.mypickerview.dataSource = self
self.pickerData = [One, Two, Three, Four]
self.mypickerview.delegate = self;

} catch {
print("bad things happened")


What could the issue be with the second code that's preventing the options in the picker view from showing up?

Answer Source

Right after

self.pickerData = [One, Two, Three, Four]

Call the following to forcibly reload the data in main thread as @SaintThread has mentioned in the comment

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download