Reputation: 35
I am building a chat app, and like Whatsapp, I need to provide users the facility to save their messages as undelivered when internet connectivity is OFF, and as soon as the internet is back then that data should be retried to be sent to the receiver.
But, in case I scheduled a write operation using Firebase .setValue(), then how could I cancel that process in case the user changes his mind and doesn't want that message to be sent, or in technical terms does not want to setValue(), how could I stop that ongoing write.
Upvotes: 2
Views: 573
Reputation: 138834
As I understand from your scenario, the problem arises when the device is offline and you want to let the user the possibility to delete the message, so it cannot be displayed once it regains connectivity. Because the setValue() method returns a Task<Void>
object and because I don't see any cancel method in this class, the only thing to do it to simply remove the message. In this case, when the user is back online, no other users will see the deleted message.
On the other side, it's true that there is a chance in which the user sends the message and right after that he loses connectivity. Even if there was a cancel method, you would have nothing to cancel, as the message is already set on the server. Again, the single option that you have is to delete the message. The delete operation will remain in queue until the device regains connectivity and right after the synchronization the message will be deleted.
Besides that, remember to set the persistence enabled, because it's not enabled by default:
setPersistenceEnabled(true);
Upvotes: 1