Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用swift 3中的计时器正确地重新加载json页面?_Ios_Json_Uitableview_Swift3_Reloaddata - Fatal编程技术网

Ios 如何使用swift 3中的计时器正确地重新加载json页面?

Ios 如何使用swift 3中的计时器正确地重新加载json页面?,ios,json,uitableview,swift3,reloaddata,Ios,Json,Uitableview,Swift3,Reloaddata,我使用的是swift 3,我有一个从json加载数据的表视图。问题是当我转到另一个页面并返回主页时,数据有时不会再次加载。我问了这个问题,我的一个朋友告诉我使用计时器,因为你需要重新加载tableView,所以你需要删除tableView上的所有数据,然后再次使用json,然后重新加载tableView,这需要几秒钟的时间,所以我使用删除tableView中的所有数据,使用计时器大约2秒钟,然后下载json并重新加载表视图,但有时它不能正常工作-有时数据没有删除,有时所有数据都将删除,我看不到任

我使用的是swift 3,我有一个从json加载数据的表视图。问题是当我转到另一个页面并返回主页时,数据有时不会再次加载。我问了这个问题,我的一个朋友告诉我使用计时器,因为你需要重新加载tableView,所以你需要删除tableView上的所有数据,然后再次使用json,然后重新加载tableView,这需要几秒钟的时间,所以我使用删除tableView中的所有数据,使用计时器大约2秒钟,然后下载json并重新加载表视图,但有时它不能正常工作-有时数据没有删除,有时所有数据都将删除,我看不到任何东西

我的代码太大了,所以我只在这里写必要的代码

  override func viewDidLoad() {
      super.viewDidLoad()

     let refreshTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(Home2ViewController.refreshFunction) , userInfo: nil, repeats: true)

     refreshFunction()

     DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {

         refreshTimer.invalidate()

     })


func refreshFunction() {
    NSLog("refreshFunction")
    print("refreshing!")

    ////////////////////// Get Home Page

    EmailSignInViewController.step.removeAll()

    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute: {
    let urlPath3: String = "http://mamamoniya.com/api/orders?api_token=\(EmailSignInViewController.api_token)"

    let url3: NSURL = NSURL(string: urlPath3)!
    let request3: NSMutableURLRequest = NSMutableURLRequest(url: url3 as URL)

    request3.httpMethod = "GET"

    let queue3:OperationQueue = OperationQueue()

    NSURLConnection.sendAsynchronousRequest(request3 as URLRequest, queue: OperationQueue.main) {(response, data, error) in
        print(NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!)

        do {
            if let jsonResult3 = try JSONSerialization.jsonObject(with: data!, options: []) as? Array<Any> {

                for item in jsonResult3 {

                    if let dict = item as?  NSDictionary {
                        if let step = dict.value(forKey: "step"){
                            EmailSignInViewController.step.append(step as! Int)

                        }
                    }
                }
            }
        } catch let error as NSError {
            print(error.localizedDescription)
        }
    }

    //////////////////End Get


    self.customTableView.reloadData()

        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3), execute: {
            self.waitingSpinner.stopAnimating()
            self.waitingTitle.textColor = UIColor.clear
        })

    })

}
override func viewDidLoad(){
super.viewDidLoad()
让refreshTimer=Timer.scheduledTimer(时间间隔:1.0,目标:self,选择器:#选择器(Home2ViewController.refreshFunction),用户信息:nil,重复:true)
刷新函数()
DispatchQueue.main.asyncAfter(截止日期:.now()+.seconds(1))执行:{
refreshTimer.invalidate()无效
})
func refresh函数(){
NSLog(“刷新功能”)
打印(“刷新!”)
//////////////////////获取主页
EmailSignInViewController.step.removeAll()
DispatchQueue.main.asyncAfter(截止日期:.now()+.seconds(2))执行:{
让urlPath3:字符串=”http://mamamoniya.com/api/orders?api_token=\(EmailSignenViewController.api_令牌)
让url3:NSURL=NSURL(字符串:urlPath3)!
让request3:NSMutableURLRequest=NSMutableURLRequest(url:url3作为url)
request3.httpMethod=“GET”
让queue3:OperationQueue=OperationQueue()
NSURLConnection.sendAsynchronousRequest(请求3作为URLRequest,队列:OperationQueue.main){(响应、数据、错误)在
打印(NSString(数据:数据!编码:String.encoding.utf8.rawValue)!)
做{
如果让jsonResult3=尝试JSONSerialization.jsonObject(使用:data!,选项:[])作为?数组{
对于jsonResult3中的项{
如果让dict=项目作为NSDictionary{
如果let step=dict.value(forKey:“step”){
EmailSignenViewController.step.append(步骤为!Int)
}
}
}
}
}将let错误捕获为NSError{
打印(错误。本地化描述)
}
}
//////////////////结束
self.customTableView.reloadData()
DispatchQueue.main.asyncAfter(截止日期:.now()+.seconds(3))执行:{
self.waitingSpinner.stop设置动画()
self.waitingTitle.textColor=UIColor.clear
})
})
}

由于某些原因,我无法在此处写入真正的url,但您可以使用任何url,正如您所知

为什么要使用计时器?只需调用您的服务,当您得到响应时,只需更新数据源并重新加载tableView。tableView没有正确重新加载,因此我删除了所有数据并将新数据附加到tableView,这将花费大量时间me秒,所以我使用计时器,因为删除数据和添加新数据需要时间。您可以在ViewWillAppear方法中重新加载数据。我想在代码中使用一些类似于promise方法的东西