【发布时间】:2015-02-27 20:28:44
【问题描述】:
我正在编写一个在终端中运行的快速脚本,该脚本向后台线程调度几个操作。没有任何额外的努力,在我完成所有调度之后,代码到达文件末尾并退出,同时也终止了我的后台操作。在我的后台操作完成之前保持 swift 脚本处于活动状态的最佳方法是什么?
我想出的最好的方法如下,但我不认为这是最好的方法,甚至是正确的。
var semaphores = [dispatch_semaphore_t]()
while x {
var semaphore = dispatch_semaphore_create(0)
semaphores.append(semaphore)
dispatch_background {
//do lengthy operation
dispatch_semaphore_signal(semaphore)
}
}
for semaphore in semaphores {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
}
【问题讨论】:
标签: swift asynchronous command-line