How to cancel Firebase setValue while pending for completion (when offline)
If there is a write queue, you can empty it by calling FIRDatabase.purgeOutstandingWrites()
.
How to cancel firebase setValue() intentionaly for android
You can use the same for android as well
purgeOutstandingWrites
The Firebase Database client automatically queues writes and sends
them to the server at the earliest opportunity, depending on network
connectivity.In some cases (e.g. offline usage) there may be a large
number of writes waiting to be sent. Calling this method will purge
all outstanding writes so they are abandoned.
All writes will be purged, including transactions and onDisconnect()
writes. The writes will be rolled back locally
, perhaps triggering
events for affected event listeners, and the client will not (re-)send
them to the Firebase backend.
How deal with Firebase offline mode and data push?
Following what @Andrew-lee and @Frank-fan-puffelen (thank to both agree on that please), the solution appear to be :
- Create a unique id with push() command
- Add a listener (Single Value) to a firebase ref of the previous created node in (1.)
Schedule your job inside the listener (here update the UI, but could be another command)
findViewById(R.id.button7).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Firebase temp = new Firebase(getString(R.string.firebase_url)).child("stack").push();
temp.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
((TextView) findViewById(R.id.textView7)).setText(String.valueOf(counter) + " - " + dataSnapshot.getKey().toString());
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
temp.setValue(counter++);
}
});
Detect local data update in Firebase
I understand that Firebase will make an effort to sync with the server when connectivity is restored, but if the app restarts that change is lost forever.
The default behavior is indeed that the pending writes are kept in memory. You can however easily change that by enabling disk persistence.
Firebase.getDefaultConfig().setPersistenceEnabled(true);
But since you indicate you don't want to use that, the alternative is to use a CompletionListener to detect when the changes have been committed to the database on Firebase's servers:
ref.setValue("I'm writing data", new Firebase.CompletionListener() {
@Override
public void onComplete(FirebaseError firebaseError, Firebase firebase) {
if (firebaseError != null) {
System.out.println("Data could not be saved. " + firebaseError.getMessage());
} else {
System.out.println("Data saved successfully.");
}
}
});
Note that completion listeners are not persisted to disk. So even if you enable dis persistence, the completion callbacks will not fire when pending writes are fulfilled after an app restart. So don't try to mix disk persistence or completion listeners.
Clear firebase persistence after logout
clearPersistence()
was launched in v6.2.0.
via
https://github.com/firebase/firebase-js-sdk/issues/449
java Firebase: delay exit until writes finish
Taken from this google group discussion. This uses a boolean done
(an AtomicBoolean for thread safe ops in this case) and a while
loop.
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import java.util.Date;
import java.util.concurrent.atomic.AtomicBoolean;
public class Demo {
public static void main(String[] args) {
final AtomicBoolean done = new AtomicBoolean(false);
Firebase ref = new Firebase("https://testjava.firebaseio-demo.com/");
ref.setValue(new Date().toString(), new Firebase.CompletionListener() {
@Override
public void onComplete(FirebaseError firebaseError, Firebase firebase) {
done.set(true);
}
});
while (!done.get());
}
}
Firebase synchronisation of locally-modified data: handling errors & global status
In the example above, what kind of errors should the fredRef.remove() callback expect to receive?
Client is offline (network connection lost) ?
No, this will not cause an error to be passed to the completion listener. It will simply cause the completion listener to not be called (yet).
Firebase server is temporarily overloaded or down for maintenance, but will be available again soon?
No. This is essentially the same as being without a network connection.
Permission denied (due to security rules) ?
Yes, this is will indeed cause an error to be passed to the completion handler.
Database location does not exist?
No, this will not cause an error to be caused to the completion listener.
If isConnected is true, does this also mean that all modified data has been synced to the server? i.e. Does "connected" also mean "synced"?
No it does not. .info/connected
will fire with true when a connection is made to the database.
If not, how can the app determine the global sync status of the whole Firebase client?
There is currently no way to determine whether your local data is up to date with the server.
Is there a special /.info/synchronized location?
No, such a location doesn't exist.
Does the app need to manually keep track of the sync success/failure result of every onComplete callback?
That depends on the use-case. But if you want to simply know when all your writes are executed, push a dummy value and wait for that to complete. Since Firebase executes the writes in order, you can be certain at that stage that you've gotten the other events.
Related Topics
Swift-Setting a Physics Body Velocity by Angle
Firebase Crashlytics Not Showing Crash Report in Console Dashboard Swift
How to Change Text Color of Actionsheet in Swiftui
How to Get Part of a String in Swift
Avplayerviewcontroller Black Screen When Swiping on iOS 11
Swiftui: How to Switch to a New Navigation Stack with Navigationviews
Swift: Specialize Method of Generic Class for Function Types
Tap Gesture Not Working as Expected When Added to Uiview in Collectionview Cell
Convert Half Precision Float (Bytes) to Float in Swift
How to Pass a Class and Method to Create an Instance of a Class
How to Make a Segue to Second Item of Tab Bar
Unwrapping Optional in Swiftui View
How to Get Mouse Location with Swiftui
Swift - Exit Outer Function from Closure
Is It Safe to Force Unwrap an Optional After Checking It Is Not Nil