Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel firebase function

Is it possible to cancel Firebase HTTPS Callable function during it's request? I have a function with some predictive search. It is called each time when user inputs a character in a search field.

Code:

func startSearch(_ query: String, completion: @escaping (_ results: [SearcheResults]) -> Void) {

    let data = [
        "query": query
    ]

    functions.httpsCallable("startSearch").call(data) { (result, error) in

        if error != nil {
          completion([])
      } else if let data = result?.data {
            // some data manipulations
            completion(elements)
        }
    }
}

Or maybe somehow dismiss earlier completions? Because for now, if user is very rapid and enter text, for example "Berlin" - completion will fire 6 times. I'd like to have a way to cancel a function or cancel previous completions.

Thanks in advance.

like image 602
Pete Streem Avatar asked Apr 26 '26 01:04

Pete Streem


1 Answers

You should try debounce, basically in debounce before you fire a request you wait for short span(eg:- 2 secs), and if user types in that span again, timer is reset to again 2 secs,check the link

Once the call is made it cannot be cancelled, It will Either get executed to completition or timedout.

like image 78
ked Avatar answered Apr 27 '26 14:04

ked



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!