2014-12-04 19 views
0

您好我正在下載JSON在我的應用程序與NSJSONSerialization的設備,似乎當我更新JSON文件並重新運行下載JSON函數它不下載新JSON。它只是下載非更新的JSON文件。我試過在不同的設備上運行應用程序,它第一次運行,但一旦我再次更新JSON,它不會下載新的JSON。我認爲它可能會被緩存,但我不確定。NSJSONSerialization沒有更新(可能獲得緩存)

這裏是我的代碼:

func downloadJSON(){ 

    let p = NSURLRequest(URL: NSURL(string: "https://themartini.co/pythonStuff/quotey.json")!) 

    let sharedSession = NSURLSession.sharedSession() 

    let dwn = sharedSession.dataTaskWithRequest(p, completionHandler: { (data : NSData!,re : NSURLResponse!, error: NSError!) -> Void in 

     if error == nil { 

      let js : AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as NSArray // downloads JSON and puuts it into an array 

      println(js) 



      dispatch_async(dispatch_get_main_queue()){ 

       self.quotes = js as NSArray 

       NSNotificationCenter.defaultCenter().postNotificationName("dataTaskFinished", object: nil) 

      } 

     } 

    }) 

    dwn.resume() 

} 

任何想法?謝謝。

+0

...你已經消除了中間CDN的原因是什麼?如何迅速,您在設備切換到設備?真正的解決將是讓你的服務器提供正確的緩存策略。 – Tommy 2014-12-04 00:49:03

回答

1

你應該通過正確的緩存策略您NSURLRequest。有一個alternative constructor包括此參數。

let p = NSURLRequest(URL: url, 
        cachePolicy: ReloadIgnoringLocalCacheData, 
        timeoutInterval: 15.0) 
+0

NSURLSession和NSURLConnection與上述任務相同。 – Mundi 2014-12-04 00:47:22

0

你可以添加一個cachebuster到你的NSURLRequest。 (URL更改爲https://themartini.co/pythonStuff/quotey.json?23421231231

其中數字是在結束的字符串是隨機的,這將打破緩存。此外,而不是使用NSURLSession,看看NSURLConnection.SendAsynchroniousRequest

+0

每次調用JSON下載函數時,我會在iOS端的URL末尾更改數字嗎?謝謝。 – mlevi 2014-12-04 00:20:46

+0

是的。這是主意。緩存依靠URL來返回相同的數據。另外,嘿在那裏Yismo。 – Moshe 2014-12-04 00:23:11

+0

@Moshe所以我基本上是隨機生成數字並將其添加到URL?另外,你好! – mlevi 2014-12-04 00:25:25