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

后端 未结 4 1071
梦毁少年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:25

    Or you can use operation queues, too. In Swift 3:

    let queue = OperationQueue()
    
    queue.addOperation() {
        // do something in the background
    
        OperationQueue.main.addOperation() {
            // when done, update your UI and/or model on the main queue
        }
    }
    

    Either this, or GCD, which Andy illustrated, work fine.

    See Apple's Concurrency Programming Guide for the relative merits of operation queues and dispatch queues (aka Grand Central Dispatch, GCD). While that guide is still illustrating the examples using Objective-C, the API and concepts are basically the same in Swift (just use the Swift syntax). The documentation for GCD and operation queues in Xcode describes both Objective-C and Swift APIs.


    By the way, you'll notice that in both the above example as well as Andy's GCD demonstration, we used "trailing closures". For example, if you look at the definition addOperationWithBlock, that is defined as a function with one parameter which is a "closure" (which is analogous to a block in Objective-C):

    func addOperation(_ block: @escaping () -> Swift.Void)
    

    That might lead you to assume that you would invoke it as follows:

    queue.addOperation({
        // do something in the background
    })
    

    But when the last parameter of a function is a closure, the trailing closure syntax allows you to take that final closure parameter out of the parentheses of the function, and move it after the function, yielding:

    queue.addOperation() {
        // do something in the background
    }
    

    And because there's nothing left in the parentheses, you can even go one step further, and remove those empty parentheses:

    queue.addOperation {
        // do something in the background
    }
    

    Hopefully that illustrates how to interpret the NSOperationQueue/OperationQueue and/or GCD function declarations and use them in your code.

    0 讨论(0)
  • 2021-01-30 11:25

    Here is the best resource to learn in detail about Councurrency

    0 讨论(0)
  • 2021-01-30 11:26

    This library lets you describe concurrency in a super expressive way:

    func handleError(_ error) { ... }
    
    HoneyBee.start(on: DispatchQueue.main) { root in
        root.setErrorHandler(handleError)
            .chain(function1) // runs on main queue
            .setBlockPerformer(DispatchQueue.global())
            .chain(function2) // runs on background queue
            .branch { stem in
                stem.chain(func3) // runs in parallel with func4
                +
                stem.chain(func4) // runs in parallel with func3
            }
            .chain(func5) // runs after func3 and func4 have finished
            .setBlockPerformer(DispatchQueue.main)
            .chain(updateUIFunc)
    }
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题