在我的代码中,我需要为邮件实施即时垃圾邮件操作。为此我在不等待服务器响应的情况下更改了我的消息。但有时在服务器请求错误上我需要将我的消息状态恢复为原始状态。 handler.removeCallbacks(runnable)
不让我知道runnable是否已经运行。我怎么能做到这一点?我还对通过RxJava或其他技术执行这些延迟作业的其他建议持开放态度。
var handler = Handler()
fun spamMessage(originalMessage: ContentMessage) {
var runnable: Runnable? = null
val modifiedMessage = originalMessage.cloneUsingJson()
if (modifiedMessage != null) {
modifiedMessage.setSpamReported(true)
runnable = Runnable {
updateMessage(message)
}
updateWithDelay(runnable)
}
ApiClient.service.spamMessage(message.serverId).subscribeAsync(subscriptions, object : ApiSubscriberNew<Void>() {
override fun onFailure() {
var canceled : Boolean = handler.removeCallbacks(runnable)
if (!canceled) {
//if couldn't cancel, or was late, revert the changes
revertMessageUpdate(originalMessage)
}
}
})
}
private fun updateWithDelay(runnable: Runnable) {
handler.postDelayed(runnable, 100)
}