3

I am trying to get stock quotes from Yahoo using Swift 3. Although there are some decent tutorials on Swift 2, none of them seem to translate well to Swift 3.

The issue I have at the moment is that in the code below, the session.dataTask is never called. The print statement never fires and the rest of the code that is in there does not work.

I have checked that the request variable looks good, and the url has been tested on the yahoo developer site.

So I'm thinking I must have the syntax of the dataTask wrong or has an error so is skipping completely.

Any thoughts?

    urlString = "http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol IN ('APL')"

        //let urlNSS : NSString = urlString as NSString
        let urlStr : String = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
        let url : URL = URL(string: urlStr as String)!
        let request = URLRequest(url: url)
        let session = URLSession.shared
        let config = URLSessionConfiguration.default

        let task = session.dataTask(with: request, completionHandler: {(data, response, error) -> Void in

            print("in the task")

            ..
            ..
        )}

task.resume()

If I inspect task I see the following enter image description here

Michael Moulsdale
  • 1,488
  • 13
  • 34
  • Is this a command-line program? Then you need a run loop: http://stackoverflow.com/questions/25126471/cfrunloop-in-swift-command-line-program. – Or a Playground? Then this should help: http://stackoverflow.com/questions/24058336/how-do-i-run-asynchronous-callbacks-in-playground. – Martin R Nov 26 '16 at 16:58

1 Answers1

4

You must resume the task (task.resume()) that you have created. By default, a task is in suspended state.

I have created a Playground file with a different Yahoo RSS Feed URL. https://drive.google.com/file/d/0B5nqEBSJjCriWl9UTWcxSE42Yk0/view?usp=sharing

The URL you have in your question is not giving any data.

    <error xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:lang="en-US">
<description>No definition found for Table yahoo.finance.quotes</description>
</error>

Code as Below:

    //: Playground - noun: a place where people can play

import UIKit
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
var str = "Hello, playground"

let yahooURLString = "https://feeds.finance.yahoo.com/rss/2.0/headline?s=yhoo&region=US&lang=en-US"
let yahooRSSURL: URL = URL(string: yahooURLString)!

var request = URLRequest(url: yahooRSSURL)
request.httpMethod = "GET"

let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let task = session.dataTask(with: request) {data, response, err in
    print("Entered the completionHandler")
    print("Response JSON:\n\(String(data: data!, encoding: String.Encoding.utf8)!)")
}
task.resume()

Hope this helps.


Edit: Attaching a screenshot of print statements appearing in the console of playgorund. enter image description here

Pranav Jaiswal
  • 3,752
  • 3
  • 32
  • 50
  • thanks for the reply. If I run this in a simulator I don't get any errors, but neither of the print statements fire either. so either there is an issue with how i'm calling the function, tasks don't allow a print statement, in the middle of the code, or simulators don't like this type of code? – Michael Moulsdale Nov 26 '16 at 18:22
  • also if i put a breakpoint after task.resume() and examine task it has no values in any of the components – Michael Moulsdale Nov 26 '16 at 18:26
  • @MichaelMoulsdale Did you download & check playground file. I have attached a Screenshot of output printed in debug console. Also clarifying prints statements works in simulator as well as inside tasks, breakpoints also work!!! – Pranav Jaiswal Nov 26 '16 at 18:28
  • yeah the playground file works fine, must be something else going on that i'm not spotting. thanks for the help on this – Michael Moulsdale Nov 26 '16 at 18:35
  • 1
    @MichaelMoulsdale I know this was a few months ago but did you solve the problem? Based on this last comment it doesn't sound like this answer solved your problem, and I think I'm having the same problem you were having. –  May 06 '17 at 05:47