我正在使用一个使用 Firebase 作为数据存储库的应用程序。我只是重构我的整个应用程序以实现 Clean Architecture 和 RxJava。以这种方式完成所有操作后,我发现管理模型对象存在问题。
这是我的问题:我有一个具有相同字段和值的 Post.class,我可以从 Firebase 数据库引用中检索这些字段和值:
public Post(Author author, String full_url, String full_storage_uri, String thumb_url, String thumb_storage_uri, String text, Object timestamp) {
this.author = author;
this.full_url = full_url;
this.text = text;
this.timestamp = timestamp;
this.thumb_storage_uri = thumb_storage_uri;
this.thumb_url = thumb_url;
this.full_storage_uri = full_storage_uri;
}
目前一切正常。当我从我的存储库类中的 Observer 检索数据时,我的问题出现了:
@Override
public Observable<List<Post>> getPosts(final String groupId){
return Observable.fromAsync(new Action1<AsyncEmitter<List<Post>>>() {
@Override
public void call(final AsyncEmitter<List<Post>> listAsyncEmitter) {
final ValueEventListener PostListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
final List<Post> postList = new ArrayList<>();
Log.e("Count ", "" + snapshot.getChildrenCount());
//For every child, create a Post object
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Post post = postSnapshot.getValue(Post.class);
Log.e("new post added ", postSnapshot.getKey());
//Set the databaseReference to the object, which is needed later
post.setRef(postSnapshot.getKey());
postList.add(post);
}
listAsyncEmitter.onNext(postList);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("The read failed: ", databaseError.getMessage());
}
};
//Remove listener when unsuscribe
listAsyncEmitter.setCancellation(new AsyncEmitter.Cancellable() {
@Override
public void cancel() throws Exception {
getPostsRef().child(groupId).removeEventListener(PostListener);
}
});
//Set the listener
getPostsRef().child(groupId).addValueEventListener(PostListener);
}
}, AsyncEmitter.BackpressureMode.BUFFER);
}
有了这个观察者,我已经管理了所有的监听器和数据调用,我唯一的问题是这些行:
//Set the databaseReference to the object, which is needed later
post.setRef(postSnapshot.getKey());
我认为将引用设置为 Post 模型中的新字段不是一个好习惯,它应该等于我的 firebase Json 树。所以我的问题是:创建两种不同的模型是一种好的做法吗?一个像“dbPost”和“PostEntity”。一个带有 firebase 值,另一个带有来自 dbPost 的构建器和我想要保存的新字段(dbReference 和可能的 valueListener)?
最佳答案
最后我所做的是创建两个不同的模型类。其中一个用于从 Firebase 检索和插入我的数据库参数,另一个用于我的应用程序使用应用程序需要的额外值来管理数据:
应用模型:
public class Post implements Parcelable{
private Author author;
private String full_url;
private String thumb_storage_uri;
private String thumb_url;
private String text;
private Object timestamp;
private String full_storage_uri;
private String ref;
private Achivement post_achivement;
private long post_puntuation;
public Post() {
// empty default constructor, necessary for Firebase to be able to deserialize blog posts
}
[......]
Firebase 模型:
public class NetworkPost {
private Author author;
private String full_url;
private String thumb_storage_uri;
private String thumb_url;
private String text;
private Object timestamp;
private String full_storage_uri;
private Achivement post_achivement;
private long post_puntuation;
public NetworkPost() {
// empty default constructor, necessary for Firebase to be able to deserialize blog posts
}
[...]
关于android - 同一 firebase 元素的多个模型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40133043/