0

I have a NSXMLParser object. I'm running an XMPP stream.

Does .parse() need to be called each time I receive a message or just once?

Here's a parse of relevant code in the NSStream:

func connectToSocket(host: String, port: Int) {

    NSStream.getStreamsToHostWithName(host, port: port, inputStream: &(self.input), outputStream: &(self.output))

    self.input!.delegate  = self
    self.output!.delegate = self



    //self.input!.scheduleInRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode)
    //self.output!.scheduleInRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode)
    self.input!.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
    self.output!.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)

    self.input!.open()
    self.output!.open()

    parser = NSXMLParser(stream: input!)
    parser!.delegate = self
}
user83039
  • 3,047
  • 5
  • 19
  • 31

1 Answers1

0

.parse() is a synchronous operation, so it will block until it has parsed the entire stream. You'll have to call parse from a different thread (or dispatch queue), or schedule the input stream in a different runloop.

See here for how to create a dispatch queue for the parser, and here for how to use the input stream from a different queue.

xnyhps
  • 3,306
  • 17
  • 21
  • With that first link, I have the parser working for about half of the messages. Any idea why it might miss some? – user83039 Nov 12 '14 at 17:24