2016-10-19 5 views
2

Firebase를 데이터 저장소로 사용하는 응용 프로그램과 함께 작업하고 있습니다. 난 그냥 클린 아키텍처와 RxJava를 구현하는 내 애플 리케이션 전체 리팩터링이야. 이 방법으로 모든 것을 수행하면 모델 객체를 관리하는 데 문제가 있음을 발견했습니다.동일한 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; 
} 

모든 미세 지금 : 내가 중포 기지 데이터베이스 참조에서 검색 할 수 있습니다 같은 필드와 값이 Post.class 있습니다. 내 저장소 클래스 내 관찰자로부터 데이터를 검색 할 때 내 문제가 나타납니다

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

: 이미 모든 청취자 및 데이터 통화를 관리하는이 관찰자와

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

, 내 유일한 문제는이 라인이다 포스트 모델에서 새로운 기준으로 참조를 설정하는 것은 좋은 습관이 아니라고 생각합니다.이 모델은 내 방화베이스 인 Json Tree와 동일해야합니다. 그래서 제 질문은 : 2 가지 모델을 만드는 것이 좋은 습관입니까? 하나는 "dbPost"와 "PostEntity"입니다. 하나는 firebase 값이 있고 다른 하나는 dbPost의 빌더이고 (dbReference 및 아마도 valueListener) 저장하려는 새 필드입니까?

누군가가 나를 도와 줄 수 있기를 바랍니다. 임씨는 더 나은 유연성을 갖춘 앱을 만들기 위해 가능한 한 아키텍처와 우수 사례를 개선하려고 노력하고 있습니다. 그래서 나는 이런 종류의 질문을하고 있습니다!

좋은 하루 되세요!

+0

가 있습니까 'key', 새로운 필드 인'String'? 'postSnapshot.getRef()'는'Reference'입니다. 내가 놓친 게 있니? –

+0

예,하지만 완전히 다른 클래스에서 Obserser.Subscribe를 호출하는 참조를 사용해야하고 게시물 목록을 수신합니다. 그리고 그 목록에서 각각의 Ref를 유지해야합니다. –

+0

왜 ValueEventListener '관찰 가능한 '흐름으로? 어쨌든 콜백을 기다려야합니다. –

답변

1

마지막으로 내가 한 것은 두 개의 다른 모델 클래스를 만드는 것입니다.

앱 모델 :

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 
} 
[......] 

검색하고 중포 기지 그것을 필요로하는 여분의 값으로 데이터를 관리하는 내 응용 프로그램에 의해 사용되는 또 다른 하나에서 내 데이터베이스 PARAMS를 삽입하는 그 중 하나는 응용 프로그램을 생각 중포 기지 모델 :

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 
} 

[...] 그냥을 설정하지 않는

관련 문제