2015-07-10 65 views
1

我使用tcp套接字從服務器接收數據(大約在一秒內完成100次),當我接收到數據時,我想將它推入數組延遲200ms,怎麼辦?如何在swift中將數據按時間推遲200毫秒

+1

檢查這些問題的答案:http://stackoverflow.com/questions/24034544/dispatch-after-gcd-in-swift –

+0

http://www.whatx.net/questions/24170282/swift-performselector- withobject-afterdelay.look at these metods – Mukesh

+0

Use dispatch_after。 – gnasher729

回答

0

您可以使用dispatch_after在指定的時間後運行閉包。你只需要確定哪些隊列您關閉應該在這裏運行,在隊列文件和他們的目的:

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

在這個例子中,我將使用其意在長時間運行的後臺任務QOS_CLASS_UTILITY。

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 
}