My Question: Can you stop a firebase document from being written after a certain time limit? I am attempting to change a user's pin and notify them of success or failure within a time limit. More on my issue below (especially the bold area at the end).
Note: Currently, a user's pin is saved in their local shared preferences. However, there may be a few tablets/phones in the same building and I want them to all have the same pin number. I plan to have the devices check for pin updates once per day. The pin is saved in Shared Preferences for immediate usage, whereas the firestore pin is only checked to update the Shared Preferences pin number.
To change their pin, I now have them enter their current pin, then test this against their app's shared preferences as well as what is saved in the firestore document. Below is all psudocode, but should be close enough to get my point across.
// MAIN ACTIVITY - Global Handler variable
Handler mHandler = new Handler();
...
// Button's OnClick()
if( (pinEntered == sharedPreferencesPin) && (pinEntered == currentFirestorePin)){
// Connect to firestore and update the new pin
// OnSuccessListener changes a Singleton boolean value to true or false
changePin(pinEntered)
// Show "Please Wait" Loading Class with spinning progress bar widget
showWaitingScreen();
// Runnable checks Singleton in 5 seconds to see whether the operation was success or failure
mHandler(checkSuccessRunnable, 5000);
}
I keep my firebase methods separate, so the next section here is a different class
// Global For PIN UPDATE Class
FirebaseFirestore db = FirebaseFirestore.getInstance();
public void changePin(String pinIn){
db.collection(pinCollectionPath).document("PIN").set(pinIn)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// Update singleton - Success
SingletonClass.setPinChangedBoolean = true;
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Update singleton - Failed
SingletonClass.setPinChangedBoolean = false;
}
});
}
Back to my Main activity I have my runnable complete. The "checkSuccessRunnable," checks the Singleton boolean value. Depending on the value, I create a success or failure Alert Dialog.
// MAIN ACTIVITY
checkSuccessRunnable= new Runnable() {
@Override
public void run() {
createAlertDialog( SingletonClass.getPinChangedBoolean() );
}
}
};
My issue arises when I try to update the firestore pin document while offline. Then, minutes later, successfully writing the document when the Tablet/Phone comes back online. I need to either have a failure or success message within 5 seconds. Obviously, the user wants to know if their pin was changed, or not, immediately.
In testing, I turned my WiFi/Data off and tried to update the Pin document. After the 5 second runnable expired, I get a "Failed to update" message, which is correct. However, after a few minutes, when I turned my phone data back on, the firestore document was immediately updated. The obvious problem is that the user already received a "failure" message after the Runnable completed (5 seconds after clicking the update button).
I originally thought about using a broadcast intent, but the app receives requests to change the current activity, by the facility hardware, every so often. Even if the success message is presented to the user later, they may never see it.