几秒钟后快速刷新 JSON 数据 [英] Refresh JSON data after a few seconds in swift
问题描述
我希望每 4 秒更新一次来自 url 的新数据,但我不知道如何做到这一点.这是我到目前为止所拥有的,它运行良好,但没有复习!Refresher 需要像 youtube 订阅者计数器一样工作,每 4 秒左右更新一次.我看过一个计时器,但我无法让它工作,因为(我认为)它是一个 searchBarSearchButtonClicked
函数并且 urlRequestid 必须有一个输入!请帮忙!谢谢!
I want this to update every 4 seconds with fresh data form the url, but i dont know how to do this. This is what i have so far and it works fine but without the refresher! The Refresher needs to work like a youtube subscriber counter that update every 4 seconds or so. I have looked at a timer but i couldn't make it work because (i think) its a searchBarSearchButtonClicked
function and the urlRequestid has to have a input! Please help! Thanks!
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
let urlRequestid = URLRequest(url: URL(string: "https://www.mylink.com/\(searchBar.text!.replacingOccurrences(of: " ", with: "%20"))/?__a=1")!)
if (interstitial.isReady){
interstitial.present(fromRootViewController: self)
interstitial = createAndLoadInterstitial()
}
let task = URLSession.shared.dataTask(with: urlRequestid) { (data, response, error) in
if error == nil {
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]
if let user = json["user"] as? [String : AnyObject] {
let profile_pic_url_hd = user["profile_pic_url_hd"] as! String
let urlstr = "\(profile_pic_url_hd)"
if var comps = URLComponents(string: urlstr) {
var path = comps.path
var pathComps = path.components(separatedBy: "/")
pathComps.remove(at: 2) // this removes the s320x320
path = pathComps.joined(separator: "/")
comps.path = path
if let newStr = comps.string {
print(newStr)
self.imgURL = "\(newStr)"
}
}
if let bio = user["biography"] as? String {
self.bioS = bio
}
if let naam = user["username"] as? String {
self.naamS = naam
}
if let followed_by = user["followed_by"] as? [String : AnyObject] {
self.VolgS = followed_by["count"] as! Int
}
if let follows = user["follows"] as? [String : AnyObject] {
self.volgD = follows["count"] as! Int
}
if let media = user["media"] as? [String : AnyObject] {
self.postS = media["count"] as! Int
}
}
if let _ = json["error"] {
self.exists = false
}
DispatchQueue.main.async {
if self.exists{
self.imgView.downloadImage(from: self.imgURL!)
self.naam.text = "@\(self.naamS ?? "")"
if self.bioS == nil {
self.bio.text = "This Person has no biography!"
} else {
self.bio.text = "\(self.bioS ?? "")"
}
self.volgers.text = "\(self.VolgS!)"
self.volgend.text = "\(self.volgD!)"
self.post.text = "\(self.postS!)"
} else {
self.exists = true
}
}
} catch let jsonError {
print(jsonError.localizedDescription)
}
}
}
task.resume()
}
}
推荐答案
一个快速但公认笨拙的解决方法是将来自 searchBarSearchButtonClicked
参数的最新 UISearchBar 实例存储在本地实例变量中:>
One quick but admittedly clumsy fix would be to store the latest UISearchBar instance from the searchBarSearchButtonClicked
parameter in a local instance variable:
var currentSearch: UISearchBar = UISearchBar()
var timer: Timer?
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
currentSearch = searchBar
// Add the rest of the method code below...
...
}
// Call this method to begin repetition
func repeatSearch() {
self.timer = Timer.scheduledTimer(withTimeInterval: 4.0, repeats: true,
block: { (timer) in
self.searchBarSearchButtonClicked(self.currentSearch)
})
}
这篇关于几秒钟后快速刷新 JSON 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!