2

I hit a web service in swift 3.0 and I am getting response in offline mode also.

Here is the details of my Work:

  1. I need to call 1 web service - let url = URL(string: "http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors")

  2. By using URLSession.shared.dataTask(...) method I called above web service.

  3. By using try? statement ' JSONSerialization.jsonObject(with: data!, options:JSONSerialization.ReadingOptions.mutableContainers) as AnyObject ' I parsed the data.

  4. I ran above code and it gives me response what I am expected (Online Mode).

  5. Now I turn off my wifi & run the above code again then I again got the same data as that in online. I put debugger on completionHandler closure on success part then I reached at that breakpoint.

  6. Please I need help. Why I am getting response in offline mode (I print array size & it shows size = 6)?

  7. Here is the code.

  8. I am working on Xcode 8.1 with Swift 3.0

{ final let urlString = "http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors"

override func viewDidLoad() { super.viewDidLoad()

    self.downloadJsonWithURL()

    print("Inside viewDidLoad")

    // Do any additional setup after loading the view, typically from a nib.
}


func downloadJsonWithURL() {

    let url = URL(string: urlString)

    let task = URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) -> Void in


        if error != nil
        {
            print("Error Found!!!!")
            return
        }

        else
        {
            guard let jsonObj = try? JSONSerialization.jsonObject(with: data!, options:JSONSerialization.ReadingOptions.mutableContainers) as AnyObject

                else
            {
                // if 'let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary' fails(i.e falls) then this block will execute.
                return
            }


            if let actorArray = jsonObj.value(forKey: "actors") as? NSArray {
                for actor in actorArray{
                    print("Actor Array = \(actorArray.count)")
                    if let actorDict = actor as? NSDictionary {

                        if let name = actorDict.value(forKey: "name") {
                            self.nameArray.append(name as! String)
                        }

                        if let name = actorDict.value(forKey: "dob") {
                            self.dobArray.append(name as! String)
                        }

                        if let name = actorDict.value(forKey: "image") {
                            self.imgURLArray.append(name as! String)
                            let data = NSData(contentsOf: URL(string: name as! String)!)
                            self.imgArray.append(UIImage(data: data as! Data)!)
                        }

                    }
                }


                OperationQueue.main.addOperation({
                    self.tableView.delegate = self as? UITableViewDelegate
                    self.tableView.dataSource = self
                    self.tableView.reloadData()

                })
            }

        }
    })


     task.resume()
}

}

shallowThought
  • 19,212
  • 9
  • 65
  • 112
Sarthak mirajkar
  • 191
  • 1
  • 1
  • 7
  • Could it be because you are using the shared data session and it is caching? – matt Jan 08 '17 at 18:36
  • Either you are using some mobile connection (G3 or such) when WIFI is off or you have NSURLSession caching results. I _thought_ it does not cache by default. See [this](http://stackoverflow.com/questions/30324394/prevent-nsurlsession-from-caching-responses) for details. – shallowThought Jan 08 '17 at 18:38
  • Please accept the answer if it solved your query. – Kunal Kumar Mar 22 '18 at 05:19

1 Answers1

3

add following code in your code.

    let urlConfig = URLSessionConfiguration.default
    urlConfig.requestCachePolicy = .reloadIgnoringLocalCacheData
    urlConfig.urlCache = nil

    let session = URLSession(configuration: urlConfig)

    let task = session.dataTask(with: url!, completionHandler: {(data, response, error) -> Void in
Nikunj Damani
  • 753
  • 3
  • 11