How to handle timeout in queries with Firebase

前端 未结 5 1677
终归单人心
终归单人心 2021-01-02 04:35

I noticed that if I execute a query in Firebase and the database server is not reachable, the callback waits just forever (or until the server is reachable again).

W

相关标签:
5条回答
  • 2021-01-02 04:49

    As per today there is no timeout concept on those listeners. One option is to manage the timeout yourself.

    This is how I do it when I also want to display a progress dialog while loading the 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){
        }
    }
    

    So when you make a request to Firebase you call showProgressDialog(true,5000) and after 5 seconds if the dialog stills there is because it could not connect and you then do what you have to as per the timeout.

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

    Hope it helps.

    0 讨论(0)
  • 2021-01-02 04:52

    I would suggest simply using a thread?

    Allow yourself to assign your call to Firebase from within a thread instance, then in the rare event that the write to Firebase takes too long you can just cancel the thread?

    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 讨论(0)
  • 2021-01-02 04:52

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

    Sample:

    Integer connectTimeoutinMillis = 6000; //6 seconds
    FirebaseOptions firebaseOptions = FirebaseOptions.builder()
     .setCredentials(credentials)
     .setDatabaseUrl(Application.firebaseSDKDatabaseUrl)
     .setConnectTimeout(connectTimeoutinMillis)
     .build();
    FirebaseApp.initializeApp(firebaseOptions);
    
    0 讨论(0)
  • 2021-01-02 04:52

    Here's my solution for the Firebase iOS SDK, this may be helpful for others:

    extension DatabaseReference {
    
        func observe(_ eventType: DataEventType, timeout: TimeInterval, with block: @escaping (DataSnapshot?) -> Void) -> UInt {
    
            var handle: UInt!
    
            let timer = Timer.scheduledTimer(withTimeInterval: timeout, repeats: false) { (_) in
                self.removeObserver(withHandle: handle)
                block(nil)
            }
    
            handle = observe(eventType) { (snapshot) in
                timer.invalidate()
                block(snapshot)
            }
    
            return handle
        }
    
    }
    

    Usage:

    database.child("users").observe(.value, timeout: 30) { (snapshot) in
    
        guard let snapshot = snapshot else {
            // Timeout!
            return
        }
    
        // We got data within the timeout, so do something with snapshot.value
    }
    
    0 讨论(0)
  • 2021-01-02 04:58

    you can manage yourself a timer controller that after x seconds remove the listener to you firebase reference. It's very simple, just one line of code in android for example.

    You can see the code for the web (Detaching Callbacks section): https://www.firebase.com/docs/web/guide/retrieving-data.html

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

    same section for IOS ;)

    0 讨论(0)
提交回复
热议问题