同一个firebase元素的多个模型类 [英] Multiple Model Classes for same firebase element
问题描述
这是我的问题:我有一个Post.class具有相同的字段和值,我可以检索从Firebase数据库参考:
public Post(Author author,String full_url,String full_storage_uri,String thumb_url,String thumb_storage_uri,String text,对象时间戳){
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< ;邮政>> 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());
//对于每个孩子,创建一个Post对象
for PostSnapshot.getValue(Post.class);
Log.e(new post added,postSnapshot.getKey());
//将databaseReference设置为稍后需要
的对象post.setRef(postSnapshot.getKey());
postList.add(post);
}
listAsyncEmitter.onNext(postList);
$ b @Override
public void onCancelled(DatabaseError databaseError){
Log.e(The read failed:,databaseError.getMessage());
}
};
//当删除
时删除侦听器listAsyncEmitter.setCancellation(new AsyncEmitter.Cancellable(){
@Override
public void cancel()throws Exception {
getPostsRef() .child(groupId).removeEventListener(PostListener);
}
});
//设置监听器
getPostsRef()。child(groupId).addValueEventListener(PostListener);
}
},AsyncEmitter.BackpressureMode.BUFFER);
$ b有了这个观察者,我已经管理了所有的监听器和数据调用,唯一的问题是这些行:
//将databaseReference设置为稍后需要的对象
post.setRef(postSnapshot .getKey());
我认为这不是一个很好的做法,将引用设置为Post模型中的新字段应该等于我的firebase Json Tree。所以我的问题是:创建两个不同的模型是一个很好的做法吗?像dbPost和PostEntity。一个用firebase的值,另一个用dbPost和我想要保存的新字段(dbReference和也许是valueListener)的构建器?
希望有人可以帮帮我!我试图尽可能地改善架构和良好的做法,以建立更好,更灵活的应用程序,这就是为什么我问这样的问题!
祝您有个美好的一天! 创建两个不同的模型类。其中之一是从Firebase中检索并插入我的数据库参数,另一个则是由我的应用程序使用额外的值来管理数据,它需要以为应用程序:
<公共类Post:实现Parcelable {
private作者author;
private String full_url;
private String thumb_storage_uri;
private String thumb_url;
私人字符串文本;
私人对象时间戳;
private String full_storage_uri;
private String ref;
私人执行post_achivement;
私人长期post_puntuation;
$ b $ public Post(){
//空的默认构造函数,Firebase必须能够反序列化博客文章
}
[......]
Firebase模型:
public class NetworkPost {
private作者author;
private String full_url;
private String thumb_storage_uri;
private String thumb_url;
私人字符串文本;
私人对象时间戳;
private String full_storage_uri;
私人执行post_achivement;
私人长期post_puntuation;
$ b $ public NetworkPost(){
//空的默认构造函数,Firebase必须能够反序列化博客文章
}
[...]
I'm working with a application that used Firebase as data repository. I'm just refactor whole my app to implement Clean Architecture and RxJava. Doing everything in this way I found a problem managing my model objects.
Here is my problem: I have a Post.class with the same fields and values that I can retrieve from Firebase database reference:
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;
}
Everything fine for now. My problem appear when I retrieve the data from my Observer in my repository class:
@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);
}
With this observer I already manage all the listeners and data calls, my only problem is these lines:
//Set the databaseReference to the object, which is needed later
post.setRef(postSnapshot.getKey());
I think that is not a good practice to set the reference as a new field in the Post Model which should equal to my firebase Json Tree. So my question is: Is a good practice to create 2 different models? One like "dbPost" and "PostEntity". One with the firebase values and the other one with a builder from a dbPost and the new fields that I want to save(dbReference and maybe valueListener)?
Hope that someone can give me a hand! Im trying to improve as much as possible in architecture and good practices to build better and more flexible apps, that's why I'm asking this kind of questions!
Have a good day!
Finally what I did was create two different model class. One of them to retrieve and insert my database params from Firebase and another one used by my app to manage the data with the extra values that it need thought the application:
App model:
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 Model:
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
}
[...]
这篇关于同一个firebase元素的多个模型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!