0

I am using helper classes with retry logic which is used throughout my app. I was wondering if there is a way I can combine the handler and callback classes into one instead of having 2 different classes one for callback and one for handler. Here's my code as follows: Retryhandler:

abstract class RetryHandler(private val totalRetries: Int = 0, private val delayMillis : Long = 0) : Handler() {

    private var retryCount: Int = 0

    fun retry(): Boolean {
        return if (retryCount++ < totalRetries) {
            if (delayMillis > 0) {
                postDelayed({ onRetry(retryCount) }, delayMillis)
            } else {
                onRetry(retryCount)
                true
            }
        } else false
    }

    abstract fun onRetry(retryCount: Int)
}

Retrycallback:

abstract class RetryableCallback(totalRetries: Int = 0, delayMillis : Long = 0)
    : RetryHandler(totalRetries, delayMillis), MyCallback {

    override fun handleTransactionCompleted() {
        if (!onCompleted()) {
            if (!retry()) {
                onFailed(null)
            }
        }
    }

    override fun handleTransactionFailed(e: MyException?) {
        if (!retry()) {
            onFailed(e)
        }
    }

    abstract fun onCompleted(): Boolean

    abstract fun onFailed(e: MyException? = null)
}

Here's how I am using them in my code:

   private val newCallback = object: RetryableCallback(5, 5000) {
            override fun onRetry(retryCount: Int) {
    ....}
    override fun onCompleted(): Boolean {
    }
    }

Any ideas ?

  • What's particularly you don't like in using two classes? One tells how you retry, second uses logic of parent to retry itself. – While True Nov 29 '18 at 14:50
  • Also it's not very clear what you are trying to achive, and what methods `handleMobileKeysTransactionCompleted` and `handleTransactionFailed` do. You want a handler, that runs some logic in a `run` method, and after a certain times of retrying calls either `onComplete` or `onFailed` method. Is that right? – While True Nov 29 '18 at 15:01
  • they are just for checking if the transaction passed or failed. I was wondering if I can integrate both into 1 class instead of creating 2 separate classes or anyway I can modify it to make it better –  Nov 29 '18 at 16:10

1 Answers1

0

Well, as long as I don't fully understand the purpose, let's say like this:

abstract class RetriableCallbackHandler(private val totalRetries: Int = 0, private val delayMillis : Long = 0) : Handler(), MyCallback {

    private var retryCount: Int = 0

    fun retry(): Boolean {
        return if (retryCount++ < totalRetries) {
            if (delayMillis > 0) {
                postDelayed({ onRetry(retryCount) }, delayMillis)
            } else {
                onRetry(retryCount)
                true
            }
        } else false
    }

    abstract fun onRetry(retryCount: Int)

    override fun handleTransactionCompleted() {
        if (!onCompleted()) {
            if (!retry()) {
                onFailed(null)
            }
        }
    }

    override fun handleTransactionFailed(e: MyException?) {
        if (!retry()) {
            onFailed(e)
        }
    }

    abstract fun onCompleted(): Boolean

    abstract fun onFailed(e: MyException? = null)
}
While True
  • 423
  • 2
  • 15
  • also, do you happen to know how to fix this one? https://stackoverflow.com/questions/53511227/how-to-avoid-memory-leaks-due-to-custom-static-handler-class?noredirect=1#comment93900039_53511227 –  Nov 29 '18 at 21:17