2

I have written a cloud function that is working well.

Sometimes this function is being executed more than one time for the same user (I made sure that only one request is being requested from the client android app).

After some debugging I noticed that this problem happens if the connection is bad. I may be or may not be correct.

How to overcome such an issue?

msamhoury
  • 333
  • 3
  • 12
  • How sure are you that your function is only called once from your app ? What happens if your cloud function fails e.g a timedout ? – Mo Nazemi Nov 04 '15 at 15:02
  • I'm 100% sure, since it is working fine. However on low connection this issue is happening. I'm thinking if this problem is related to the fact that the network libraries actually fires multiple requests for one single request we make so in case one of those failed the other doesn't. What do you think? @MoNazemi – msamhoury Nov 04 '15 at 20:40
  • I do not think the network library will fire twice for a request. Btw where do you call the cloud function from ? an iOS app ? – Mo Nazemi Nov 05 '15 at 10:57

1 Answers1

2

As in the comments I also don't believe the client SDKs would duplicate a Cloud Function call on a bad connection. The expected behaviour would be for them to throw a network-related exception on the client side and not call it again. A problem would arise if the Cloud Function runs successfully and the client is only unable to get the result back.

I can think of the following solutions, with no more details about the Cloud Function itself:

  1. Try to make the function idempotent - Meaning that even if it runs twice the end result is the same, assuming the same input/parameters.

  2. Cache the results and manually throttle the function - This is more complicated, and is only needed if the network problem persists and you can't work around eliminating the side effects of the function. You would have to create another Class to cache the results of the function (maybe keyed by the parameters) and returning the same result with no computation and side effects.

paolobueno
  • 1,678
  • 10
  • 9