0

after hours of search I need your help. I have setup a program for iOS showing recipes of my private cooking book. The recipes are stored in a json file on my web server. I use another application on my MAC to update the recipes in this json file and to store it on my web server. Unfortunately the updated recipes are not shown in my iOS-application. The iOS-app shows always the old data - I guess they are stored locally on my iPhone/iPad after the first installation of the app?

How can I enforce that at every launch of the iOS-app on my iPhone/iPad the actual json data from the web server are used and not the old one.

I retrieve the data via this function I call from the viewDidLoad() and before the reloadData() of the UITableView showing the titles of the recipes

 func initKochbuch() {
            let url = URL(string: selektiertesKochbuch)!
         let urlSession = URLSession.shared
         let task = urlSession.dataTask(with: url) { (data, response, error) in
             guard let data = data else {
                 debugPrint("Fehler beim Laden", error ?? "Unbekannter Fehler")
                 return
             }
             self.kochbuch_local.rezepte = try! JSONDecoder().decode([Rezept].self, from: data)
             self.initRezeptAnsicht()
            OperationQueue.main.addOperation {self.tableView.reloadData()}

         }
         task.resume()
}

What do I have to do in addition? Thanks for your support.

carlson
  • 89
  • 11
  • try reloadData() in main theard. DispatchQueue.main.async { reloadData() } – Son Pham Jul 06 '20 at 08:22
  • 1
    It is possible that the JSON is cached by your `URLSession` due to its configuration. Please see if [this](https://stackoverflow.com/a/42722401/526828) answer fixes your issue. – Matic Oblak Jul 06 '20 at 10:54
  • @Matic Oblak: thank you - this was the solution and it works now – carlson Jul 06 '20 at 13:45

1 Answers1

1

Few possibilities:

  1. hope you are not storing the data locally(coredata/plist/any file), and loading it anywhere in your app.
  2. Your web server might be caching the old data, please check once.
  3. See if you have any hardcoded sample data which was used for testing purpose but not removed after actual implementation.

Hope this helps.

Shan
  • 41
  • 1
  • Together with the hint from Matic Oblak to configure the URLSession it solved the problem to bypass cached data – carlson Jul 09 '20 at 05:19