0

I try to create a socket as shown below:

socket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0))

and I implement the func:

func socket(socket : GCDAsyncSocket, didConnectToHost host:String, port p:UInt16){
    pingTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "sendPing:", userInfo: nil, repeats: true)
}

func sendPing(){
    print("will send ping...")
}

But the sendPing will never be called, and if I try this:

socket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue())

the deletegateQueue is socket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue()), it will work fine.

The UI updating is use main queue, so I want to do socket connection with another type queue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0) seems to be a good one.

How can I make it work well?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
fcbflying
  • 693
  • 1
  • 7
  • 23
  • 2
    The selector for the timer should be `"sendPing"` (without the colon) so it matched the actual function you wrote. – rmaddy Dec 21 '15 at 04:21
  • @rmaddy I don't think it is caused by the colon, because if i use 'dispatch_get_main_queue' , it works fine. And of course i try remove the colon, still not work. – fcbflying Dec 21 '15 at 04:33
  • 4
    You should review http://stackoverflow.com/questions/14569693/timer-inside-global-queue-is-not-calling-in-ios – rmaddy Dec 21 '15 at 04:57
  • a strong +1 for rmaddy's comment. – Joshua Nozzi Dec 21 '15 at 16:23

0 Answers0