2017-06-22 166 views
0

我希望这个每4秒更新一次新的数据形式的网址,但我不知道如何做到这一点。这是我到目前为止,它工作正常,但没有进修!刷新器需要像每四秒钟更新一次的YouTube用户计数器一样工作。我看了一个计时器,但我无法使它工作,因为(我认为)它的searchBarSearchButtonClicked函数和urlRequestid必须有一个输入!请帮忙!谢谢!在几秒钟后刷新JSON数据

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() 
} 

}

+0

你想打这个API每4秒钟? 它是第一次正常工作? –

+0

是的,4秒后等。这样您在4秒后就可以获得新的数据。所以是的,我想在4 seocnds后打这个api –

回答

1

一个快速的,但不可否认笨拙的解决将是最新的UISearchBar实例从searchBarSearchButtonClicked参数存储在本地实例变量:

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) 
    }) 
} 
+0

我有2个错误。第一个错误:**“无法将'String'类型的值转换为期望的参数类型''UISearchBar”**此错误是由'searchBarSearchButtonClicked(currentSearch) '。第二个错误是:**无法指定'UISearchbar'类型的值来键入'String'**。这个错误是由'currentSearch = searchBar'! –

+0

@DaniKemper对不起,现在应该是无错的 –

+0

没有任何错误。但它仍然不起作用。或者我没有看到数据在4秒后刷新/变形! (数据应该是chaging) –

-1

您可以通过使用Timer实现它,它计划为每4秒。

DEMO

对于iOS 10.0及以上

var timer: Timer? 
func callMe() { 
    func doSomrThing(str: String) { 
     print(str) 
    } 

    doSomrThing(str: "first time") 

    self.timer = Timer.scheduledTimer(withTimeInterval: 4.0, repeats: true, block: { (timer) in 
     doSomrThing(str: "after 4 second") 
    }) 
} 

对于低于iOS的10.0

var timer: Timer? 
func callMe() { 

    self.doSomeThing(str: "first time") 

    self.timer = Timer.scheduledTimer(timeInterval: 4.0, target: self, selector: #selector(AddTextVC.timerHandler), userInfo: nil, repeats: true) 
} 

func doSomeThing(str: String) { 
    print(str) 
} 

func timerHandler() { 
    self.doSomeThing(str: "after 4 seconds") 
} 

根据演示只需更换你的代码。 而这个代码添加到您的viewController:

deinit { 
    self.timer?.invalidate() 
    self.timer = nil 
}