当用户点击按钮时,我执行15个具有某些功能的后台任务:
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
myfunc()
}
所以我在手机上测试了这个,当我点击按钮几次时,我有很多线程和电话滞后一分钟......
所以我需要使用任务队列创建数组,例如将线程数限制为5或10,或者swift有标准工具吗?
答案 0 :(得分:3)
感谢您的评论,这是有效的:
let backgroundQueue = NSOperationQueue()
backgroundQueue.maxConcurrentOperationCount = 5
for i in 0...100 {
backgroundQueue.addOperationWithBlock(){
self.somefunc()
}
}