I have an app with different API operations made with retrofit. Unfortunately, the API I am using doesn't have an update function so instead of updating, I am currently deleting, and adding with updated values.
This solution works OK when my update clicks are more than half a second apart, but when I click faster It causes duplications.
I am using Data Binding and MVVM. So, function call route is as following: XML -> Fragment (Adapter in this case) -> ViewModel -> Repository
fun updateCartItem(cart: Cart) {
apiService.deleteCartItem(cart.cartFoodId,cart.orderUsername).enqueue(object: Callback<CartResponse> {
override fun onResponse(call: Call<CartResponse>?, response: Response<CartResponse>?) {
apiService.addFoodToCart(
cart.foodName,
cart.foodPicture,
cart.foodPrice,
cart.foodQuantity,
cart.orderUsername
).enqueue(object: Callback<CartResponse> {
override fun onResponse(call: Call<CartResponse>?, response: Response<CartResponse>?) {
fetchCart(cart.orderUsername)
}
override fun onFailure(call: Call<CartResponse>?, t: Throwable?) {}
})
}
override fun onFailure(call: Call<CartResponse>?, t: Throwable?) {}
})
}
I tried to add @Synchronized annotation to my function, but it didn't help.
Basically what I need is a way to ensure my update function is working only once.