Contact資料模型之EntityDelta

來源:互聯網
上載者:User

public class EntityDelta implements ParcelableEntityDelta的資料模型描述:EntityDelta是ValuesDelta的集合,它還實現了Parcelable介面。它包含了主要資料(存在ValuesDelta
mValues中)和子資料.它把子資料以ValuesDelta的形式按照它們的mimetype存放在HashMap<String, ArrayList<ValuesDelta>> mEntries中(擁有相同的mimetype的ValuesDelta放在同一個ArrayList).注1:EntityDelta可用於RawContact。主要資料正好對應RawContact的主表,子資料正好對應RawContact的子表。關於RawContact的更多內容參照《 RawContacts》注2:ValuesDelta是EntityDelta的一個內部類。更多關於ValuesDelta的,可以參考《 Contact資料模型之ValuesDelta》建構函式public EntityDelta()建立一個空的EntityDeltapublic EntityDelta(ValuesDelta values)建立一個EntityDelta,並用ValuesDelta values來初始化主要資料。主要成員變數:private ValuesDelta mValues;用於存放主表(主要資料),private HashMap<String, ArrayList<ValuesDelta>> mEntries = Maps.newHashMap();用於存放子表(子資料),子資料以ValuesDelta的形式按照它們的mimetype存放在其中。靜態成員變數public static final Parcelable.Creator<EntityDelta> CREATOR 註:該變數因Parcelable而設計。主要靜態函數:public static EntityDelta fromBefore(Entity before)用Entity before來執行個體化一個EntityDelta,把Entity before的getEntityValues()得到主要資料以ValuesDelta.fromBefore的形式打包成ValuesDelta並賦予mValues.且設定mValues的mIdColumn為RawContacts._ID。然後用before.getSubValues()得到子資料,然後以ValuesDelta.fromBefore的形式打包成ValuesDelta並放入mEntries。注1:這裡的Entity before對應一個RawContacts的資料。注2:該函數在把Entity轉化成EntityDelta的過程中,把Entity的子資料的Uri uri丟失了。因為對於update操作可以通過ValuesDelta的id就可以確定它的位置(當然Uri uri也可以確定,然而Data.CONTENT_URI是固定,所以可以丟棄Uri uri)。對於刪除操作,有主要資料的資訊就可以了。public static EntityDelta mergeAfter(EntityDelta local, EntityDelta remote) 用EntityDelta remote對EntityDelta
local進行mergeAfter操作。它主要是通過對EntityDelta local和EntityDelta remote中的ValuesDelta進行相應的ValuesDelta的mergeAfter操作來實現mergeAfter操作。對於主要資料是直接對他們進行mergeAfter操作。對於子資料,是對同id(即ValuesDelta的mIdColumn值相同)的ValuesDelta進行mergeAfter操作。最後返回新的EntityDelta
local。注1:EntityDelta local可以為null,這時會建立一個空的EntityDelta以便處理。注2:mergeAfter操作是針對EntityDelta local而進行,所以它會被改變。注3:關於ValuesDelta的mergeAfter操作的更多內容可參考《 Contact資料模型之ValuesDelta》主要成員函數:public ValuesDelta getValues()返回主要資料mValues。public boolean isContactInsert() 返回是否是Insert操作,通過調用主要資料mValues的isInsert()方法來判斷。public ValuesDelta getPrimaryEntry(String mimeType) 從子表集合變數mEntries,返回PrimaryEntry得到MIME類型為mimeType的PrimaryEntry。找到MIME類型為mimeType的ValuesDelta且它的isPrimary()為true,就返回該ValuesDelta。如果沒找到,但MIME類型為mimeType的ValuesDelta的ArrayList<ValuesDelta>至少有一個,則返回該列表上的第一個ValuesDelta。否則返回null。public ValuesDelta getSuperPrimaryEntry(String mimeType) 從子表集合變數mEntries,返回SuperPrimaryEntry 它直接調用getSuperPrimaryEntry(mimeType, true)。public ValuesDelta getSuperPrimaryEntry(String mimeType, boolean forceSelection) 從子表集合變數mEntries,返回SuperPrimaryEntry 原理和getPrimaryEntry(String mimeType)相似。但是只有forceSelection為true的情況下,才在沒有找到SuperPrimaryEntry的情況下返回PrimaryEntry,如果PrimaryEntry也沒找到,才返回MIME類型為mimeType的ValuesDelta的ArrayList<ValuesDelta>的第一的ValuesDelta,如果ArrayList<ValuesDelta>為空白則返回null。private ArrayList<ValuesDelta> getMimeEntries(String mimeType, boolean lazyCreate)  從子表集合變數mEntries,得到指定MIME類型mimeType的ArrayList<ValuesDelta>,如果lazyCreate為true,在IME類型mimeType的ArrayList<ValuesDelta>沒被建立的情況下,會建立它並放入HashMap<String,
ArrayList<ValuesDelta>> mEntrie中。public ArrayList<ValuesDelta> getMimeEntries(String mimeType) 直接調用getMimeEntries(mimeType, false)。public int getMimeEntriesCount(String mimeType, boolean onlyVisible) 從子表集合變數mEntries,得到指定MIME類型mimeType的ValuesDelta數量。如果onlyVisible是true,則ValuesDelta還必須是isVisible()的。public boolean hasMimeEntries(String mimeType) 返回HashMap<String, ArrayList<ValuesDelta>>中是否有MIME類型mimeType的ArrayList<ValuesDelta>。public ValuesDelta addEntry(ValuesDelta entry) 向子表集合變數mEntries加入一個ValuesDelta entry。public ValuesDelta getEntry(Long childId)  從子表集合變數mEntries返回id為Long childId的ValuesDeltapublic int getEntryCount(boolean onlyVisible) 得到子表集合變數mEntries中ValuesDelta的數量,如果onlyVisible是true,則ValuesDelta還必須是isVisible()的。public boolean equals(Object object) 返回兩個EntityDelta是否相當。只有他們所包括的ValuesDelta一樣才相等。(這裡包括主要資料ValuesDelta mValues和子資料HashMap<String, ArrayList<ValuesDelta>>)註:這裡的"一樣"是指ValuesDelta的equals(Object object)返回true.private boolean containsEntry(ValuesDelta entry) 用於檢測ValuesDelta entry是否已經在子表集合變數mEntries中。public void markDeleted()  對包含的所有ValuesDelta進行markDeleted()操作。(這裡包括主要資料ValuesDelta mValues和子資料HashMap<String, ArrayList<ValuesDelta>>)public String toString()把所有ValuesDelta進行轉化為String. private void possibleAdd(ArrayList<ContentProviderOperation> diff,ContentProviderOperation.Builder builder) 在builder不為空白的情況下,把builder產生的ContentProviderOperation加入到ArrayList<ContentProviderOperation> diff中。 注1:該方法其實應該是static的才對。不知道為什麼google沒把它寫成靜態。也許是google的失誤吧。 public void buildAssert(ArrayList<ContentProviderOperation> buildInto)     /**     * Build a list of {@link ContentProviderOperation} that will assert any     * "before" state hasn't changed. This is maintained separately so that all     * asserts can take place before any updates occur.     */ 用主要資料mValues的Id和RawContacts.VERSION生產一個RawContacts.CONTENT_URI上的AssertQuery形builder,並加入ArrayList<ContentProviderOperation>
buildInto)。註:該函數的真正意義應該是在對資料庫操作時(Provide中)進行資料庫更新時確保資料的一致性。即資料在我們讀到EntityDelta並沒有改變過。 public void buildDiff(ArrayList<ContentProviderOperation> buildInto) 該函數負責把該EntityDelta轉化為ArrayList<ContentProviderOperation>,以便進行資料庫操作。它主要是調用ValuesDelta的buildDiff來進行ContentProviderOperation,然後把他們整合起來。 注1:對於全新的插入操作,需要在操作前把主表mValues的RawContacts.AGGREGATION_MODE設定為RawContacts.AGGREGATION_MODE_SUSPENDED,等子資料都插入完了才設定為RawContacts.AGGREGATION_MODE_DEFAULT。 注2:對於更新操作,但是子資料有插入操作的同上。 注3:如果主要資料時插入操作,那麼子資料也必須是插入操作,否則會拋異常。 注4:對於全EntityDelta的刪除操作,只需要主表的資訊就可以了,因為它的每個子表都存有主表的Id(即Data.RAW_CONTACT_ID). 注5:建立AssertQuery形的builder,可用這樣的形式:final ContentProviderOperation.Builder builder = ContentProviderOperation.newAssertQuery(RawContacts.CONTENT_URI) protected Builder buildSetAggregationMode(Long beforeId, int mode) 用Long beforeId, int mode產生update的Builder。 public int describeContents() 返回0.// Nothing special about this parcel 註:該方法來自Parcelable。 public void writeToParcel(Parcel dest, int flags)  把主表和子表資料都寫到Parcel。 註:該方法來自Parcelable。 public void readFromParcel(Parcel source) 從Parcel source中讀取主表和子表資料。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.