【问题标题】:Firebase Firestore Distributed Counter - No Properties to Serialize FoundFirebase Firestore 分布式计数器 - 找不到要序列化的属性
【发布时间】:2018-08-27 11:11:09
【问题描述】:

我已经根据post 中的建议阅读并更改了我的代码。

但是,我的问题仍然存在,得到同样的错误:

java.lang.RuntimeException: No properties to serialize found on class com.blush.android.blush.chat.chatCloudCommunications.ChatCloudSentMessageToFirestore$Counter

我在一个名为 ChatCloudSentMessageToFirestore 的公共类中运行以下所有内容。我省略了在同一个类中定义的“createCounter”、“getCounter”和“incrementCounter”。我这样做是为了保持代码简短,并且因为它们与 Firebase Firestore 网站上定义的相同:https://firebase.google.com/docs/firestore/solutions/counters 所以不太可能是错误的原因(除非他们需要我不知道的任何修改的)。

public class Counter {
    int numShards;

    public Counter(int numShards) {
        this.numShards = numShards;
    }
}

// counters/${ID}/shards/${NUM}
public static class Shard {
    int count;

    public Shard(){}

    public Shard(int count) {
        this.count = count;
    }
}

我在同一个班级也有这个参考:

DocumentReference counterChrisDocRef = db.collection("users")
        .document("ypiXrobQxuZ0wplN5KO8gJf934")
        .collection("conversations")
        .document("conversation0") //Specified conversation
        .collection("messageCounter")
        .document("shards");

我正在尝试在此方法中使用计数器:

public void addSentMessageToFirestoreDB(final Map<String, Object> messageSent) {
    WriteBatch batch = db.batch();

    createCounter(counterChrisDocRef, 1);

    incrementCounter(counterChrisDocRef, 1);


    DocumentReference chrisSentMessageRef = db.collection("users")
            .document("ypiXrobQxuZ0wplN5KO8gJf934")
            .collection("conversations")
            .document("conversation0") //Specified conversation
            .collection("messages")
            .document("message" +  getCount(counterChrisDocRef) + " (sent)");

    batch.set(chrisSentMessageRef, messageSent);

    batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {

        }
    });

【问题讨论】:

    标签: java android firebase google-cloud-firestore counter


    【解决方案1】:

    因为您根本没有为您的类中的字段使用公共 getter,所以请将您的所有字段设置为 public,因为您的代码现在是默认值。为了直接在您的字段上设置值,您的字段必须是public。如果您想让您的字段私有,只需添加相应的公共 getter,一切都会正常工作。

    还请从您的 Shard 类中删除 static 关键字。没有必要创建那个类static。

    【讨论】:

      猜你喜欢
      • 2017-11-12
      • 1970-01-01
      • 2019-05-17
      • 2016-10-11
      • 2018-02-25
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多