T
T
Tsuzukeru2020-12-21 20:53:02
Java
Tsuzukeru, 2020-12-21 20:53:02

How to call retry() on an asynchronous method by changing the call parameters of that method?

There is an asynchronous method AsyncMethod(param1:String, param2:String, apiKey:String) , one of whose parameters is an Api key. This key is in the Singleton class ApiKeyStore , as a field. This method calls the server and returns a Json response. There is a limit for each Api key, and when it is reached, AsyncMethod() gives a 403 error
. In this case, in doOnError(), I access the ApiKeyStore and change the Api key to a new one. Further along the call chain, the retry() method is triggered in order to repeat the call to AsyncMethod, but this method is called with the same Api key , although the key has changed in the ApiKeyStore.

//примерный код
AsyncMethod(param1,param2,ApiKeyStore.CurrentKey)
.doOnError{
ApiKeyStore.getNextApiKey()
}
.retry(ApiKeyStore.attemptsNumber)


It seems that the problem is related to the memory area where the method parameters are stored. How can this problem be solved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2020-12-21
@Tsuzukeru

Everything is going right. AsyncMethod(param1,param2,ApiKeyStore.CurrentKey) is called only once, and every retry resubscribes to its result. Therefore, the key does not change. You need to make sure that the method itself is called during retry, which means using Observable.defer:

Observable.defer { AsyncMethod(param1,param2,ApiKeyStore.CurrentKey) } 
.doOnError{
    ApiKeyStore.getNextApiKey()
}
.retry(ApiKeyStore.attemptsNumber)

Now, when retrying, the resubscription will be to defer, and AsyncMethod will be called each time with new parameters.
Something like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question