How to handle timeout in requests using Firebase

I noticed that if I execute a request in Firebase and the database server is unavailable, the callback will wait just forever (or until the server is available again).

If this behavior is quite natural for the asynchronous approach used, it would nevertheless be useful to have a simple way to specify a timeout so that you can inform the user about the status.

Is there such an option, and I just missed it - or is it really missing? Or how would you solve this problem?

+7
source share
4 answers

you can control the timer controller, which after x seconds removes the listener using the firebase link. It is very simple, only one line of code in android, for example.

You can see the code for the Internet (section "Disabling callbacks"): https://www.firebase.com/docs/web/guide/retrieving-data.html

or for android (section "Disable callbacks"): https://www.firebase.com/docs/android/guide/retrieving-data.html#section-detaching

same section for iOS;)

+3
source

There is currently no timeout concept for these listeners. One option is to manage the timeout yourself.

Here's how to do it when I also want to display a progress dialog when loading content.

private void showProgressDialog(boolean show, long time) { try { if (progressDialog != null) { if (show) { progressDialog.setMessage("Cargando..."); progressDialog.show(); new Handler().postDelayed(new Runnable() { public void run() { if(progressDialog!=null && progressDialog.isShowing()) { progressDialog.dismiss(); Toast.makeText(ActPreguntas.this, "Couldn't connect, please try again later.", Toast.LENGTH_LONG).show(); } } }, time); } else { progressDialog.dismiss(); } } }catch(IllegalArgumentException e){ }catch(Exception e){ } } 

Therefore, when you make a request in Firebase, you call showProgressDialog (true, 5000) and after 5 seconds if the dialog remains there because it cannot connect, and then you do what you have, according to the timeout.

In the Firebase listener callback, you do this showProgressDialog (false, 0)

Hope this helps.

+3
source

Would I suggest just using a theme?

Allow yourself to assign your Firebase call from a stream instance, and then in the rare case when writing to Firebase takes too much time, can you just cancel the stream?

 let thread = NSThread(target:self, selector:#selector(uploadToFirebase), object:nil) 

,,

 func uploadToFirebase(data: Dictionary) { // Do what you need to here. Just an example db.collection("posts").document("some unique post id").setData([ "name": "John", "likes": 0 ]) { err in if let err = err { print("Error writing document: \(err)") } else { print("Document successfully written!") } } } 

Then just create a timer that cancels the thread if the timer fires. If not , just cancel the timer.

0
source

If you use the Firebase SDK v6.5.0 and higher, you can use the FirebaseOptions setConnectTimeout ( https://firebase.google.com/docs/reference/admin/java/reference/com/google/firebase/FirebaseOptions.Builder.html#setConnectTimeout (int) ).

Example:

 Integer connectTimeoutinMillis = 6000; //6 seconds FirebaseOptions firebaseOptions = FirebaseOptions.builder() .setCredentials(credentials) .setDatabaseUrl(Application.firebaseSDKDatabaseUrl) .setConnectTimeout(connectTimeoutinMillis) .build(); FirebaseApp.initializeApp(firebaseOptions); 
0
source

All Articles