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)?
答案 0 :(得分:1)
最后我做的是创建两个不同的模型类。其中一个用于从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
}
[...]
答案 1 :(得分:0)
@Exclude注释可用于排除某些字段更新为firebase,因此您可以使用这些排除字段来存储您自己的数据,例如post.ref
我的应用中的示例:
class LocationRecord {
public final Integer id;
public final String title;
public final String address;
public final Double latitude;
public final Double longitude;
@Exclude
public Integer distance;
&#13;