NSThread sleepfortimeinterval blocks main thread

前端 未结 3 1522
庸人自扰
庸人自扰 2020-12-31 05:44

I want to simulate a communication with a server. As the remote server will have some delays I want to use a background thread that has on it

 [NSThr         


        
3条回答
  •  星月不相逢
    2020-12-31 06:20

    Swift:

    let nonBlockingQueue: dispatch_queue_t = dispatch_queue_create("nonBlockingQueue", DISPATCH_QUEUE_CONCURRENT)
    dispatch_async(nonBlockingQueue) {
        NSThread.sleepForTimeInterval(1.0)
        dispatch_async(dispatch_get_main_queue(), {
            // do your stuff here
        })
    }
    

提交回复
热议问题