【问题标题】:cURL get data doesn't update when data on server has changed当服务器上的数据发生更改时,cURL 获取数据不会更新
【发布时间】:2016-02-14 22:54:07
【问题描述】:

我有一个从网站 api 获取数据的函数。

func getData() {
print("getData is called")
//create authentication ... omitted

//create authentication url and request
let urlPath = "https://...";
let url = NSURL(string: urlPath)!
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.setValue("Basic \(base64EncodedCredential)", forHTTPHeaderField: "Authorization")
request.HTTPMethod = "GET"

//some configuration here...

let task = session.dataTaskWithURL(url) { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
    let json = JSON(data: data!)
}
    task.resume()
}

我能够从 API 获取数据,并且我有一个观察者,因此每次当应用程序进入前台时,都可以再次调用 getData() 函数来更新数据。

override func viewDidLoad() {
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "willEnterForeground", name: UIApplicationWillEnterForegroundNotification, object: nil)
}

func willEnterForeground() {
     print("will enter foreground")
     getData()
}

我很确定当我的应用程序进入前台时,会再次调用 getData(),但是,即使服务器 API 上的数据发生更改,我的数据也不会更新。我试图关闭应用程序并再次打开它,但它仍然没有更新数据。所以我想知道是否有人可以给我一些建议。任何帮助将不胜感激!

【问题讨论】:

  • 我在let task = session.dataTaskWithURL(url) { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in let json = JSON(data: data!) } 中添加了一些断点,似乎我的代码试图获取数据(api 每分钟更改一次数据),但是即使我观察到数据,它也会得到旧数据服务器 api 已更改。如果我将应用程序长时间发送到后台(大约 15 分钟),当它被带到前台时,数据会更新。

标签: swift api curl background-foreground


【解决方案1】:

你需要 a 使它成为一个带有完成的闭包。这将使它能够发出请求,然后在您实际从请求中取回数据后调用 reload "Table View"。这是您需要在 getData() 函数上更改的内容

func getData(completion: (json) -> Void)) {
     print("getData is called")
     //create authentication url and request
     let urlPath = "https://...";
     let url = NSURL(string: urlPath)!
     let request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
     request.setValue("Basic \(base64EncodedCredential)", forHTTPHeaderField: "Authorization")
     request.HTTPMethod = "GET"

     //some configuration here...

     let task = session.dataTaskWithURL(url) { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
           let json = JSON(data: data!)
     }
       completion(json)
}

然后您需要调整调用此函数的位置,因为您更改了参数,它应该看起来更像这样。

func willEnterForeground() {
    print("will enter foreground")

    getData(completion:{
        self.data = json
        self.tableView.reloadData()
    })
}

这将等到数据返回,然后重新加载视图,更新所有内容以反映服务器上的内容。

如果您还有其他问题,请告诉我。

【讨论】:

  • 嗨@Daniel,这不起作用,即使在视图didEnterForeground 时再次调用getData 函数,我仍然遇到同样的问题。当session.dataTaskWithURL(url) 完成时,我使用dispatch_async(dispatch_get_main_queue()) 更新标签
  • 啊,我刚刚意识到您需要使用完成块使您的请求成为一个闭包,这样您就不会尝试重新加载数据,直到您真正拥有它。
  • 您能解释一下为什么要添加完成处理程序吗? dataTaskWithURL 已经使用完成处理程序来更新标签,以便在任务完成后更新视图。正如我在 cmets 中提到的,通过添加断点,我注意到我的代码确实到服务器获取数据并在服务器中打印它们,问题是获取的数据并不总是最新的。而且我认为应该是func getData(completion: (json: JSON) -> Void)) 而self.data = json 是什么?我没有那个财产。谢谢丹尼尔!
【解决方案2】:

我终于发现是因为我没有删除缓存。更改缓存策略即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-23
    • 2020-09-29
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 2014-04-24
    相关资源
    最近更新 更多