I'm following the documentation to create a distributed counter in Firebase Firestore, however, I'm getting an error on their provided code.
// counters/${ID}
public class Counter {
int numShards;
public Counter(int numShards) {
this.numShards = numShards;
}
}
// counters/${ID}/shards/${NUM}
public class Shard {
int count;
public Shard(int count) {
this.count = count;
}
}
When running the createCounter method they've defined
public Task<Void> createCounter(final DocumentReference ref, final int numShards) {
// Initialize the counter document, then initialize each shard.
return ref.set(new Counter(numShards))
.continueWithTask(new Continuation<Void, Task<Void>>() {
@Override
public Task<Void> then(@NonNull Task<Void> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
List<Task<Void>> tasks = new ArrayList<>();
// Initialize each shard with count=0
for (int i = 0; i < numShards; i++) {
Task<Void> makeShard = ref.collection("shards")
.document(String.valueOf(i))
.set(new Shard(0));
tasks.add(makeShard);
}
return Tasks.whenAll(tasks);
}
});
}
I simply get a Runtime Exception
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.enterprises.optaku.bvalyan.gametalk, PID: 8471
java.lang.RuntimeException: No properties to serialize found on class com.enterprises.optaku.bvalyan.gametalk.TopicVotingFragment$Counter
at com.google.firebase.firestore.g.zzg$zza.<init>(SourceFile:643)
at com.google.firebase.firestore.g.zzg.zza(SourceFile:331)
at com.google.firebase.firestore.g.zzg.zzb(SourceFile:152)
at com.google.firebase.firestore.g.zzg.zza(SourceFile:1085)
at com.google.firebase.firestore.UserDataConverter.convertPOJO(SourceFile:427)
at com.google.firebase.firestore.DocumentReference.set(SourceFile:165)
at com.enterprises.optaku.bvalyan.gametalk.TopicVotingFragment.createCounter(TopicVotingFragment.java:217)
at com.enterprises.optaku.bvalyan.gametalk.TopicVotingFragment.lambda$null$5$TopicVotingFragment(TopicVotingFragment.java:180)
Even when I serialize the classes, I still get an exception in their code when I go to increment the counter
Shard shard = transaction.get(shardRef).toObject(Shard.class);
The compiler complains there is no no-argument constructor in the Shard class.
I'm at a loss here, as this is the only documentation I can find. Has anyone implemented this successfully and know what I could be missing here?
As the error message says, your Shard class has no no-argument constructor. JavaBean type classes must have a no-argument constructor in order for it to be instantiated by code that can't fully understand what the other constructors do. So, you should add a no-arg constructor to your code:
Without that constructor, the Firestore SDK doesn't have a predictable way to create instances of
Shard
.