I'm creating an app for iOS devices which will monitor price of one stock. I've successfully created a function which collets the data from an API and prints out the current value of stock.
The problem is that when I run the app one (current) price gets printed and it stays like that all the time.
My question is how do I make that function repeat itself every specified amount of time?
This is my viewDidLoad and is the function I want to repeat every couple of seconds.
Code:
import Cocoa
class ViewController: NSViewController {
let baseURL = "url"
@IBOutlet weak var bitcoinValue: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
getJSON { (usdPrice) -> Void in
let usdPriceText = usdPrice.description
self.bitcoinValue.stringValue = usdPriceText
print(usdPrice)
}
}
func getJSON(completion: (Double) -> Void) {
let url = NSURL(string: baseURL)
let request = NSURLRequest(URL: url!)
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
if error == nil{
let swiftyJSON = JSON(data: data!)
let usdPrice = swiftyJSON["bpi"]["USD"]["rate"].doubleValue
completion(usdPrice)
} else {
print("There was an error!")
}
}
task.resume()
}
}