简体   繁体   中英

Thread queue in Swift

When user click at button i perform 15 background tasks with some function:

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 ?

Thanks for comments, this is works:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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