2015-08-31 22 views
1

@Realm:RLMArray <>每次返回不同的實例

爲什麼我的RLMArray在運行時會給出不同的對象實例?

這是我如何定義我的RLMArray:

@property RLMArray<HouseImage> *images; 

我添加/刪除HouseImage對象數組的所有時間(交易)。

當我訪問圖片爲:每次我得到不同的情況下,時間

_house.images[indexPath.row] 

(lldb) p _house 
(RLMAccessor_v0_House *) $52 = 0x00000001701a5780 
2015-08-30 23:02:07.695 Apt Note[5992:1308601] Loading image named: 1C178A31-5F33-4CD3-9B7C-B026DF7A5E19_2 
2015-08-30 23:02:07.731 Apt Note[5992:1308601] Loading image named: CFD99689-12C4-49CB-AAB6-850FFCD902D7_3 
2015-08-30 23:02:07.750 Apt Note[5992:1308601] Loading image named: 194D55EA-125A-4CFC-8CCF-758E929BE7D5_4 

// This is the first time when iterating the array. The [house] object remains same, but not the images array items. 
(lldb) p _house 
(RLMAccessor_v0_House *) $53 = 0x00000001701a5780 
(lldb) p _house.images[0] 
(RLMAccessor_v0_HouseImage *) $54 = 0x00000001740bbf60 
2015-08-30 23:02:36.947 Apt Note[5992:1308601] Loading image named: 1C178A31-5F33-4CD3-9B7C-B026DF7A5E19_2 
2015-08-30 23:02:36.986 Apt Note[5992:1308601] Loading image named: CFD99689-12C4-49CB-AAB6-850FFCD902D7_3 
2015-08-30 23:02:37.031 Apt Note[5992:1308601] Loading image named: 194D55EA-125A-4CFC-8CCF-758E929BE7D5_4 

// This is the second iteration 
(lldb) p _house 
(RLMAccessor_v0_House *) $55 = 0x00000001701a5780 
(lldb) p _house.images[0] 
(RLMAccessor_v0_HouseImage *) $56 = 0x00000001740bc140 

如果你看我的日誌中,house對象保持不變。但是,RLMArrayimages)中的實例已經無處改變。除了這段代碼之外,沒有人在這種情況發生時正在讀/寫領域。

有沒有人知道爲什麼會發生這種情況?

如果我不清楚,請告訴我,我會盡力解釋清楚。

回答

3

這是預期的行爲。爲了實現零拷貝存儲系統,Realm根本不保存實際數據。 Realm使用持久化屬性的屬性訪問器來動態獲取屬性,因此當您多次訪問屬性時,它實際上每次都會根據設計返回不同的實例。 RLMArray也是如此。所以每次你想訪問RLMArray的元素時,Realm都會通過創建一個不同的代理對象來返回。

供參考:如果對象沒有持久,RLMArray每次都返回相同的實例。因爲RLMArray只是由NSArrayhttps://github.com/realm/realm-cocoa/blob/master/Realm/RLMArray.mm#L153-L159支持。

但是一旦對象被持久化,RLMArray更改爲RLMArrayLinkView。由於上述原因,RLMArrayLinkView每次都會返回不同的實例。 https://github.com/realm/realm-cocoa/blob/master/Realm/RLMArrayLinkView.mm#L193-L197

相關問題