简体   繁体   English

Swift中的线程队列

[英]Thread queue in Swift

When user click at button i perform 15 background tasks with some function: 当用户点击按钮时,我执行15个具有某些功能的后台任务:

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

So i have tested this on my phone, and when i click on button couple times, i got a lot of threads and phone lagged for a minute... 所以我在手机上测试了这个,当我点击按钮几次时,我有很多线程和手机滞后一分钟......

So do i need create Array with task queue and for example limit number of threads to 5 or 10 or swift has standard instruments for this ? 那么我需要创建带有任务队列的数组,例如将线程数限制为5或10,或者swift有这个标准工具吗?

Thanks for comments, this is works: 感谢您的评论,这是有效的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM