Multiple model classes for a single firebase element

I am working with an application using Firebase as a data store. I just reorganize my entire application to implement Clean Architecture and RxJava. Performing everything in this way, I discovered a problem with managing model objects.

Here is my problem: I have Post.class with the same fields and values ​​that I can extract from the Firebase database link:

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

All perfectly. My problem arises when I retrieve 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 the following lines:

  //Set the databaseReference to the object, which is needed later post.setRef(postSnapshot.getKey()); 

I think it is not a good practice to set the link as a new field in the Post model, which should match my Json Tree base. So my question is: is it good practice to create two different models? One is like "dbPost" and "PostEntity". One with firebase values ​​and the other with a builder from dbPost and new fields that I want to save (dbReference and possibly a Listener value)?

Hope someone can give me a hand! I am trying to improve as much as possible in architecture and best practices to create better and more flexible applications, so I ask such questions!

Have a nice day!

+2
source share
1 answer

Finally, I made two different model classes. One of them for extracting and pasting my database parameters from Firebase and the other used by my application to manage data with additional values ​​that it needs, was invented by the application:

Application 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 } 

[...]

+1
source

Source: https://habr.com/ru/post/1258031/


All Articles