3

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.

Pete Streem
  • 360
  • 5
  • 14

2 Answers2

6

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.

Soni Sol
  • 2,367
  • 3
  • 12
  • 23
ked
  • 2,426
  • 21
  • 24
2

Once you invoke a callable function, it can't be canceled. The function will run to completion or timeout. You will need to be sure on the client that you really want to invoke the function. You are by no means obliged to consume the result (you can ignore it if you want), but the transaction will complete, unless the client app dies in the process. In that case, the function on the backend will still complete, but it will just not be able to deliver the response.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441