【问题标题】:Clojure - Get Realtime Updates with Cloud FirestoreClojure - 使用 Cloud Firestore 获取实时更新
【发布时间】:2018-05-03 08:15:21
【问题描述】:

这里是EventListener接口

package com.google.cloud.firestore;

    public interface EventListener <T> {
      void onEvent(@javax.annotation.Nullable T t, @javax.annotation.Nullable com.google.cloud.firestore.FirestoreException e);
    }

这是Java部分代码 (参考https://firebase.google.com/docs/firestore/query-data/listen

DocumentReference docRef = db.collection("cities").document("SF");
    docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
      @Override
      public void onEvent(@Nullable DocumentSnapshot snapshot,
                          @Nullable FirestoreException e) {
        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");
        }
      }
    });

这是我转换成 Clojure 的代码

(defn outlook-synced-listenning
      [^DocumentReference ref]
      (let []
        (-> ref
            (.addSnapshotListener
              (reify EventListener
                (^void onEvent [this ^DocumentSnapshot  snapshot ^FirestoreException e]

                  (if-not (nil? e)
                    (println " Listen failed: " + e)
                    )
                  (if (and (not (nil? snapshot)) (.exists snapshot))
                    (println " Current data: " + (.getData snapshot))
                    (println " Current data null ")
                    )

                )))
            )
        ))

这是错误

Caused by: java.lang.IllegalArgumentException: Can't find matching method: onEvent, leave off hints for auto match.
        at clojure.lang.Compiler$NewInstanceMethod.parse(Compiler.java:8305)
        at clojure.lang.Compiler$NewInstanceExpr.build(Compiler.java:7853)
        at clojure.lang.Compiler$NewInstanceExpr$ReifyParser.parse(Compiler.java:7754)
        at clojure.lang.Compiler.analyzeSeq(Compiler.java:6919)

我认为Clojure代码与Java接口不匹配时参数类型和类型提示的错误。

我想我仍然没有正确地将下面的代码行转换为 Clojure docRef.addSnapshotListener(new EventListener ()

请告诉我该怎么做。谢谢

【问题讨论】:

    标签: clojure google-cloud-firestore


    【解决方案1】:

    我删除了除“this”之外的所有参数类型和类型提示。效果很好

    非常感谢弗兰克·莫耶

    (defn outlook-synced-listenning
          [^DocumentReference ref]
          (let []
            (-> ref
                (.addSnapshotListener
                  (reify EventListener
                    (onEvent [this snapshot e]
    
                      (if-not (nil? e)
                        (println " Listen failed: " + e)
                        )
                      (if (and (not (nil? snapshot)) (.exists snapshot))
                        (println " Current data: " + (.getData snapshot))
                        (println " Current data null ")
                        )
    
                    )))
                )
            ))
    

    【讨论】:

      猜你喜欢
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 2021-02-13
      相关资源
      最近更新 更多