Firebase Firestore分布式计数器文档代码崩溃

时间:2018-07-24 19:07:27

标签: java android firebase google-cloud-firestore

我正在按照文档在Firebase Firestore中创建分布式计数器,但是,我在其提供的代码上遇到了错误。

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

运行createCounter方法时,它们已经定义

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

我只是遇到了运行时异常

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)

即使我序列化了类,当我增加计数器时,它们的代码仍然会出现异常

Shard shard = transaction.get(shardRef).toObject(Shard.class);

编译器抱怨在Shard类中没有无参数的构造函数。

我在这里很茫然,因为这是我能找到的唯一文档。有人成功实现了这一点,知道我在这里可能会缺少什么吗?

1 个答案:

答案 0 :(得分:1)

如错误消息所述,您的Shard类没有无参数构造函数。 JavaBean类型类必须具有无参数构造函数,以便无法完全理解其他构造函数做什么的代码将其实例化。因此,您应该在代码中添加一个无参数的构造函数:

public class Shard {
    int count;

    public Shard() {}  // this constructor has no arguments

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

没有该构造函数,Firestore SDK无法以可预测的方式创建Shard的实例。