使用Firebase的密钥和类的最佳实践

问题描述 投票:-1回答:1

第一次使用NoSQL数据库很难。我现在的问题是,很多时候我发现在没有密钥作为我的POJO中的属性的情况下编码更难。你的方法是什么?

我的POJO:

public class Trip {
    @Exclude
    private String id;
    private String cityId;
    private String tripName;
    private long arrivalDate;
    private int duration;
}

我的数据结构:

-KIqtlY8VQFCuYmi-fWc
 arrivalDate: 2310943
 cityId: "paris"
 duration: 4
 tripName: "Paris in June"

非常感谢

java firebase firebase-realtime-database pojo
1个回答
1
投票

我的方法是创建一个Key / Object对并使用该Pair而不仅仅是对象。

List<Pair<String, Trip>> mTripPairs = new ArrayList<>();
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
    String key = dsp.getKey();
    Trip trip = dsp.getValue(Trip.class);
    mTripPairs.add(new Pair<>(key, trip));
}

然后您可以随时访问密钥和对象。

String key = mTripPairs.get(0).first;
Trip trip = mTripPairs.get(0).second;
© www.soinside.com 2019 - 2024. All rights reserved.