How to reset a Completion Handler in Swift

Hunter

I have a function. It runs great but when I want to call it again it knows that that function has already been completed before so if I try to call it again it will automatically pass to completed. How do I reset the completion handler?

Code:

func Test(completionHandler: @escaping (_ finished: String?) -> Void) {
    DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
        completionHandler("finished") // tell the caller that this has completed
    }
}

I call it like this:

self.Test(completionHandler: { finished in
     print("We have completed function)")
})

If I were to call it again after about a minute. Instead of it waiting 10 seconds like its supposed. It will automatically printed line.

print("We have completed function)")

Things to know:

The code above is a test example me real code is this.

   func GoToPage(completionHandler: @escaping (_ finished: String?) -> Void) {
        self.webView.evaluateJavaScript("document.getElementById('results').getElementsByClassName('page')[0].getElementsByClassName('name')[0].innerText"){ (value, error) in
        if error != nil {
        } else {
        print(value)
        completionHandler("finished")
        }
        }
}

I call it like this:

     self.GoToPage(completionHandler: { finished in
     print("We have completed function)")
     })

Then I wait one 1-2 minutes and call it again like this the same way.

    DispatchQueue.main.asyncAfter(deadline: .now() + 130.0) {
     self.GoToPage(completionHandler: { finished in
     print("We have completed function")
     })
    }

An it prints me the line We have completed function right off the bat when called.

EXAMPLE:

This puts it all together

     self.GoToPage(completionHandler: { finished in
     print("We have completed function)")
     NextStep()
     })


     func NextStep(){
     DispatchQueue.main.asyncAfter(deadline: .now() + 130.0) {
     self.GoToPage(completionHandler: { finished in
     print("We have completed function")
     })
     }
     }


     func GoToPage(completionHandler: @escaping (_ finished: String?) -> Void) {
        self.webView.evaluateJavaScript("document.getElementById('results').getElementsByClassName('page')[0].getElementsByClassName('name')[0].innerText"){ (value, error) in
        if error != nil {
        } else {
        print(value)
        completionHandler("finished")
        }
        }
        }
Puneet Sharma

It is working as it is supposed to work.
DispatchQueue.main.asyncAfter is an async function, i.e. when you are calling the self.test, the function returns after asking main thread to asynchronously wait for 10 seconds, and then immediately you ask the function to do the same for second time.
If you want the function to wait for the first execution, one way to do this is to call the function for second time in its completion handler:

self.Test(completionHandler: { finished in
    print("We have completed function)")
    self.Test(completionHandler: { finished in
        print("We have completed function)")
    })
})

EDIT: code in playground. Prints second function after about 10 seconds.

class TestAsyncAfter {

    func nextStep(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
            self.goToPage(completionHandler: { finished in
                print("We have completed function")
            })
        }
    }

    func goToPage(completionHandler: @escaping (_ finished: String?) -> Void) {
        completionHandler("finished")
    }
}

let test = TestAsyncAfter()
test.goToPage(completionHandler: { finished in
    print("We have completed function")
    test.nextStep()
})

PlaygroundPage.current.needsIndefiniteExecution = true

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to write completion handler in Swift with animation.

From Java

Swift @escaping and Completion Handler

From Dev

Swift Completion Handler Syntax

From Dev

completion handler with parameters (Swift)

From Dev

Generic completion handler in Swift

From Dev

Completion Handler in Swift

From Dev

Completion Handler - Parse + Swift

From Dev

Completion Handler Error in Swift

From Dev

Completion Handler - Parse + Swift

From Java

How could I create a function with a completion handler in Swift?

From Dev

How can I properly document a method with a completion handler in iOS swift

From Dev

How to pass (optional) completion handler closure to transitionFromViewController in Swift?

From Dev

How to call this completion handler?

From Dev

Animate Images with completion handler in Swift

From Dev

Simple completion handler or callback in swift

From Dev

Swift 3 - completion handler not called

From Dev

calling a completion handler Firebase Swift

From Dev

How to cancel a completion handler in iOS

From Dev

Call Swift function with completion handler in objective c

From Dev

Can swift functions have a default completion handler?

From Dev

Swift show view controller from a completion handler

From Dev

Return value from completion handler - Swift

From Dev

Objective-C to Swift : Pass Completion Handler

From Dev

Swift 2.0 Alamofire Completion Handler Return Json

From Dev

Call Swift completion handler in objective c

From Dev

Replicating Swift completion handler on Android & Java

From Dev

Objective-C to Swift : Pass Completion Handler

From Dev

Return value from completion handler - Swift

From Dev

NSURLSessionDataTask return data in completion handler, Swift 2.0

Related Related

HotTag

Archive