0
我知道如何使用swift進行常規API調用。我無法理解的是如何在必要時重複API調用。向API重複請求
我想調用API每隔一秒
API調用代碼段:
let url = URL(string: "https://api.darksky.net/forecast/34eaef38915078ea03c22bb9063bd7ea/37.8267,-122.4233")
let request = URLRequest(url: url!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 10)
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: OperationQueue.main)
let task: URLSessionDataTask = session.dataTask(with: request, completionHandler: { (data: Data?, response: URLResponse?, error: Error?) in
if let error = error {
print(error)
} else if let data = data,
let dataDictionary = try! JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary {
print("API Data:")
print(dataDictionary)
}
})
task.resume()
注:這不是實際的API,我會打電話
你不唱定時課嗎? –
每1秒調用API時,設備電池會耗盡。 – Hasya
我想你需要使用時間機器請求 - https://darksky.net/dev/docs/time-machine – Hasya