How to do multithreading, concurrency or parallelism in iOS Swift?

后端 未结 4 1076
梦毁少年i
梦毁少年i 2021-01-30 11:07

Is there any way to create a worker thread in Swift?, for example, if there\'s a major functionality that requires a lot of calculations and hence causes the main thread to dela

4条回答
  •  终归单人心
    2021-01-30 11:38

    You can use Grand Central Dispatch (GCD) for such tasks.

    This is a basic example:

    let backgroundQueue: dispatch_queue_t = dispatch_queue_create("com.a.identifier", DISPATCH_QUEUE_CONCURRENT)
    
    // can be called as often as needed
    dispatch_async(backgroundQueue) {
        // do calculations
    }
    
    // release queue when you are done with all the work
    dispatch_release(backgroundQueue)
    

提交回复
热议问题