2017-07-19 57 views
1

我有以下非正規化數據結構: 一個聯繫人可以關聯多個記錄。一個記錄可以有多個關聯的聯繫人(許多< - >很多關係)。爲了跟蹤他們的關係,一個int值來表示一個特定的記錄聯繫人的角色,角色值存儲在兩個獨立的參考FirebaseIndexRecyclerAdapter - 如何從鍵參考位置獲取每個鍵的值?

Contact 
- Contact1:data 
- Contact2:data 
- Contact3:data 

Record 
- Record1:data 
- Record2:data 

Record_Role_Ref 
- Record1 
-- Contact1: roleA 
-- Contact2: roleA 
-- Contact3: roleD 
- Record2 
-- Contact1: roleB 

Contact_Role_Ref 
- Contact1 
-- Record1: roleA 
-- Record2: roleB 

我使用FirebaseIndexRecyclerAdapter是顯示相關的聯繫人到列表一個特定的記錄ID。因此,對於關鍵的參考,我會用Record_Role_Ref/RECORD_ID,併爲數據參考我會用聯繫,就像這樣:

// Setup the reference to the all the associated contact list in record_role_ref, using the record id as key 
Query mRecordRoleRef = firebaseDatabase.getReference().child(DB_RECORD_ROLE_REF).child(mRecordId); 

// Reference the Contact data ref 
Query mContactRef = firebaseDatabase.getReference().child(DB_CONTACT); 

FirebaseIndexRecyclerAdapter mContactAdapter = new FirebaseIndexRecyclerAdapter<Contact, ContactViewHolder>(Contact.class, 
     R.layout.item_contact, 
     ContactViewHolder.class, 
     mRecordRoleRef, // The Firebase database location containing the keys associated contacts to this record 
     mContactRef)// The Firebase database location to watch for data changes. Each key key found at keyRef's location represents a list item in the RecyclerView. 

限制(S):我不希望將角色值存儲在每個聯繫人都並記錄對象,因爲每當角色發生更改時,聯繫人和記錄的整個對象都會被提取和更新。用戶想要刪除,修改,移動聯繫人和記錄,並更改角色。

問題: 聯繫人的角色值作爲密鑰的值存儲在mRecordRoleRef中。 FirebaseIndexRecyclerAdapter是否可以/如何從關鍵引用中獲取值?在這種情況下,什麼是好的/最好的做法?

感謝提前:)

回答

0

截至目前,我只是形成另一個數據讀取populateViewHolder回調方法裏面請求。由於數據讀取請求本身也是異步的,因此我不確定這是否適用於大型列表以及何時回收視圖。 populateViewHolder返回的viewHolder被設置爲final。

Query mRecordContactRoleRef = firebaseDatabase.getReference().child(DB_RECORD_CONTACT_ROLE_REF).child(mRecordId).child(mContact.getContactId()); 

mRecordContactRoleRef.addListenerForSingleValueEvent(new ValueEventListener() { 
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 
         // Getting the role int base on record type 
         Long roleNum = (Long) dataSnapshot.getValue(); 
         viewHolder.setContactRoleTv("hi, the role is " + roleNum); 
        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) { 

        } 
       });