ios - Swift 中的线程队列

标签 ios multithreading swift

当用户点击按钮时,我执行 15 个具有某些功能的后台任务:

let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
    myfunc()
}

所以我已经在我的手机上测试了这个,当我点击按钮几次时,我有很多线程并且手机滞后了一分钟...

那么我是否需要创建带有任务队列的数组,例如将线程数限制为 5 或 10,或者 swift 有这方面的标准工具?

最佳答案

感谢评论,这是可行的:

let backgroundQueue = NSOperationQueue()
backgroundQueue.maxConcurrentOperationCount = 5
for i in 0...100 {
   backgroundQueue.addOperationWithBlock(){
       self.somefunc()
  }
}

关于ios - Swift 中的线程队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33775318/

相关文章:

android - Flutter Android 在没有 onPress 的情况下以编程方式关闭/关闭 ProgressHUD 对话框

ios - 如何使用设置包中的多值标题和值

iOS:如何突出显示带有蓝色模糊光环的按钮?

java - 线程中断-JCIP

ios - iOS 中的部落冲突等等待游戏中的计时器、时钟更改用户证明

ios - 从 iOS 应用程序检测用户的 watchOS 版本

multithreading - 中止线程?

c - 线程在等待变量更改时陷入无限循环

swift - 底部行的可扩展 tableView 单元格可见问题?

ios - core Data 重启应用后删除数据