So I'm using an NSURLConnection
to get JSON data from a website, it works and I store the data in a search controller/ table view. Now when a user clicks on a cell in the view I want to segue to a new view, and load another set of JSON data from a website. However, in this new view the connection never runs the methods. Here is what I'm doing both times (each in a different view controller)
func startConnection(){
var url: NSURL = NSURL(string: self.formatted)!
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
connection.scheduleInRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode) // I tried to put this in based on suggestions from searches I did, it didn't help
connection.start()
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
self.data.appendData(data)
}
func connectionDidFinishLoading(connection: NSURLConnection!){
var err: NSError
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
println(jsonResult)
}
From breakpoints it looks like it never runs:
func connection(connection: NSURLConnection!, didReceiveData data: NSData!)
I'm calling startConnection()
in ViewDidLoad
Implement
connection:didFailWithError:
, too. See what it reports. Without that, you have no way of knowing why it failed.Also, in
didReceiveData
(and the rest of these methods), theNSURLConnection
parameter is not optional. Back before these were audited, they may have used implicitly unwrapped optionals, but no longer. Double check your method signatures. If you actually define your class as explicitly conforming toNSURLConnectionDataDelegate
andNSURLConnectionDelegate
, you should get warnings about this.As an aside, you are not using the
NSError
variable inconnectionDidFinishLoading
that could be populated ifJSONObjectWithData
encountered any problems parsing the response. So supply thatNSError
variable toJSONObjectWithData
, and you can now diagnose any errors that occur during the parsing. Hopefully you will not have any parsing errors, but if you do, having theNSError
reference would be very useful.Also, you don't need that
startImmediately
offalse
and manually scheduling it on the main run loop. You only do that if you are initiating this connection from a background thread. But since you're calling this fromviewDidLoad
, that's unnecessary.So it might end up looking like:
By the way, you might also want to implement
didReceiveResponse
as that can also provide information about any server errors you may receive: