我正在使用一个使用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"one_answers"PostEntity"。一个具有firebase值,另一个具有来自dbPost的构建器和我想要保存的新字段(dbReference和valuellistener)?
最后我创建了两个不同的模型类。其中一个用于从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
}
[......]
重火力点模型:
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
}
[…]
@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;