Ich habe einen Web-Service in Swift 3.0 und ich bekomme Antwort im Offline-Modus auch. HierGet Response im Offline-Modus
sind die Details meiner Arbeit:
Ich brauche 1 Web-Service nennen - lassen url = URL (string: "http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors")
von URLSession.shared.dataTask mit (...) Methode habe ich oben Web-Service aufgerufen.
Mit versuchen? Anweisung 'JSONSerialization.jsonObject (mit: data !, Optionen: JSONSerialization.ReadingOptions.mutableContainers) als AnyObject' Ich habe die Daten analysiert.
Ich lief über Code und es gibt mir Antwort, was ich erwartet werde (Online-Modus).
Jetzt schalte ich meine wifi & den obigen Code erneut ausführen, dann habe ich wieder die gleichen Daten wie im online. Ich habe den Debugger auf den Abschluss gestellt, den Handler auf den Erfolgsteil, dann habe ich den Breakpoint erreicht.
Bitte ich brauche Hilfe. Warum bekomme ich Antwort im Offline-Modus (Ich drucke Array-Größe & zeigt Größe = 6)?
Hier ist der Code.
Ich arbeite an Xcode 8.1 mit Swift 3,0
{ final urlString = "http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors"
Überschreibung func viewDidLoad() lassen { 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()
}
}
Könnte es sein, weil Sie die gemeinsame Datensitzung verwenden und es Caching ist? – matt
Entweder verwenden Sie eine mobile Verbindung (G3 oder eine ähnliche), wenn WIFI ausgeschaltet ist oder Sie NSURLSession-Caching-Ergebnisse haben. Ich denke, es wird standardmäßig nicht zwischengespeichert. Details finden Sie unter [this] (http://stackoverflow.com/questions/30324394/prevent-nsurlsession-from-caching-responses). – shallowThought