0

I have a NSOperation subclass with the following main function:

override func main() {
    if self.cancelled {
        return
    }

    var stringResponse: String!
    var urlString: String!
    if self.arduinoConnection.arduinoHTTPPort == 80 {
        urlString = String(format: "http://%@/arduino/%@/%i/", arguments: [self.arduinoConnection.arduinoAddress, self.arduinoConnection.pinType.lowercaseString, self.arduinoConnection.arduinoPin])
    } else {
        urlString = String(format: "http://%@:%i/arduino/%@/%i/", arguments: [self.arduinoConnection.arduinoAddress, self.arduinoConnection.arduinoHTTPPort, self.arduinoConnection.pinType.lowercaseString, self.arduinoConnection.arduinoPin])
    }
    let url = NSURL(string: urlString)
    let request = NSURLRequest(URL: url!)
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
        println(NSString(data: data, encoding: NSUTF8StringEncoding))
        stringResponse = NSString(data: data, encoding: NSUTF8StringEncoding) as! String

        if (stringResponse == "") {
            self.arduinoConnection.state = .Failed
            self.arduinoConnection.switchState = false
            self.arduinoConnection.statusText = "Failed To Connect"
        } else {
            self.arduinoConnection.state = .Downloaded
            if (stringResponse == "0") {
                self.arduinoConnection.state = .Downloaded
                self.arduinoConnection.switchState = false
                self.arduinoConnection.statusText = "Connected"
            } else if (stringResponse == "1") {
                self.arduinoConnection.state = .Downloaded
                self.arduinoConnection.switchState = true
                self.arduinoConnection.statusText = "Connected"
            }
        }
    }
}

My operation's state is finished before NSURLConnection.sendAsynchronousRequest's completion handler is actually ruined. How to make NSOperation wait until the completion handler of NSURLConnection.sendAsynchronousRequest fires?

Nikita Zernov
  • 5,465
  • 6
  • 39
  • 70
  • The operation is already on a background thread. So make the call synchronous not asynchronous. The reason it is getting finished is because the run loop on that thread has finished executing. You need to keep it executing on that thread. (You can mess around with stuff so that you don't need this but making it synchronous is the easiest solution). – Fogmeister May 18 '15 at 19:21

1 Answers1

0

You should override start instead of main. You will have to manually fire off the notifications for isExecuting and isFinished. Documentation here.

Dima
  • 23,484
  • 6
  • 56
  • 83
  • `isExecuting` and `isFinished` are read-only properties as in https://developer.apple.com/library/ios/documentation/Cocoa/Reference/NSOperation_class/index.html#//apple_ref/occ/instm/NSOperation/start – Nikita Zernov May 18 '15 at 19:18
  • That's true. You can subclass `NSOperation` and make them read/write as you can see here: http://stackoverflow.com/questions/24109701/nsoperation-property-overrides-isexecuting-isfinished – Dima May 18 '15 at 20:17