简体   繁体   中英

Firebase Firestore Distributed Counter - No Properties to Serialize Found

I've already read and changed my code according to what is recommended in this post .

However, my problem still persists, getting the same error:

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

I'm running everything below inside a public class called ChatCloudSentMessageToFirestore . I've left out the "createCounter", "getCounter" and "incrementCounter" which are defined in the same class. I do this in order to keep the code short, and because they're identical to what's defined on the Firebase Firestore website: https://firebase.google.com/docs/firestore/solutions/counters so are unlikely to be the cause of the error (unless they need any modification that I'm unaware of).

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;
    }
}

I also have this reference within the same class:

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

and I'm trying to use the counters in this method:

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) {

        }
    });

Because you aren't using public getters for your fields in your classes at all, please make all your fields public because in your code are now default. In order to set values directly on your fields, your fields must be public . If you want to have your fields private, just add the corresponding public getters and everything will work fine.

Please also remove the static keyword from your Shard class. There is no need to make that class static .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM