1

I use tcp socket to receieve the data from server(about 100 times in one second) , When I receieve the data ,I want to push it into an array by delay 200ms, How to do?

jansma
  • 1,455
  • 1
  • 13
  • 20

1 Answers1

0

You can use dispatch_after to run a closure after a specified amount of time. You just need to determine which of the queues your closure should run on, documentation on the queues and their purpose here:

https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/#//apple_ref/c/func/dispatch_get_global_queue

For this example I'll use QOS_CLASS_UTILITY which is intended for long running background tasks.

let qos = Int(QOS_CLASS_UTILITY.value)
let delayInMilliseconds = 200
let delay = Int64(delayInMilliseconds * Double(NSEC_PER_MSEC))
let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, delay)
dispatch_after(dispatchTime, dispatch_get_global_queue(qos,0)) {
    //code to push data into array 
}
Jackalope
  • 86
  • 4