Search code examples
swiftclosuresblock

how to call the block inside the function, who is waiting for another function's block completion inside itself?


I have a situation where in a class I have 3 functions. a, b and c function. I am calling c function inside b and b function inside a. but here I have 1 question. where to call b function completion block, once the c's function completion block is executed completely.

for eg:

typealias fetchCompletionBlock = () -> Void

class abcd {

    func a() {
        b({
            print("b called completed")
        })
    }
    func b(_ onCompletion: @escaping fetchCompletionBlock) {
        c({
            print("c called completed")
        })
    }
    func c(_ onCompletion: @escaping fetchCompletionBlock) {
        print("c called")

        onCompletion()
    }
    //self.a()
}

var data = abcd()
data.a()

where to call the b function completion block. i know it will be inside b function body, but i don't know the exact place.

Could some one help in this.


Solution

  • b function completion block should be called with in the c function completion handler.

    function b should be like:

    func b(_ onCompletion: @escaping fetchCompletionBlock) {
        c({
            print("c called completed")
            onCompletion()
        })
    }