Cannot get real-time updates with Cloud Firestore

Viewed 67

I am trying to get realtime updates with Cloud Firestore. While reading and writing are successful, I cannot get any update notification with the following code in Kotlin:

    val collectionName = "users"
    val docId = "alovelace"
    val docRef = db.collection(collectionName).document(docId)
    docRef.addSnapshotListener({ doc, e ->
      System.out.println("Updated")
      if (doc != null) {
          if (doc.exists()) {
            System.out.println("Current data: " + doc.getData())
          }
          else {
            System.out.println("Current data: null")
          }
      }
      else {
          e!!.printStackTrace()
      }
    })

I can read from docRef and therefore this is a valid DocumentReference:

    System.out.println("Get " + docRef.get().get().getData())
    //print Get {last=Lovelace, born=1915, first=Bob}

I also followed another sample code to wrap it as a function and return when there is one snapshot, but still in vain

    fun listenToDocument(
      db: Firestore,
      collection: String,
      document: String
    ): Map<String, Any>{
    val future = SettableApiFuture.create<Map<String, Any>>();

    // [START listen_to_document]
    val docRef = db.collection(collection).document(document)
    docRef.addSnapshotListener(object: EventListener<DocumentSnapshot> {
      override fun onEvent(
        snapshot: DocumentSnapshot?,
        e: FirestoreException?
      ) {
        if (e != null) {
          System.err.println("Listen failed: " + e);
          return;
        }

        if (snapshot != null && snapshot.exists()) {
          System.out.println("Current data: " + snapshot.getData());
        } else {
          System.out.print("Current data: null");
        }
        // [START_EXCLUDE silent]
        if (!future.isDone()) {
          future.set(snapshot!!.getData());
        }
        // [END_EXCLUDE]
      }
    });
    // [END listen_to_document]

    return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS)
  }

    Thread( fun() {
      val update = listenToDocument(db, collectionName, docId)
      System.out.println(update)
    }).start()

I updated the fields of the document from the web interface as well as the following code:

    val writeDocRef = db.collection(collectionName).document(docId)
    // Add document data  with id "alovelace" using a hashmap
    val data = HashMap<String, Any>()
    data.put("first", "Ada")
    data.put("last", "Lovelace")
    data.put("born", 1815)
    //asynchronously write data
    val result = writeDocRef.set(data)
    // result.get() blocks on response
    System.out.println("Update time : " + result.get().getUpdateTime())

The writing is also successful, but I just cannot see any outputs from the onEvent function, i.e. it is never called. I looked through a few other examples, including the official guides but they are all similar. And I did not see anyone complains about it...please help.

0 Answers
Related