@领域:

为什么我的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对象保持不变。但是,RLMArray(images)中的实例无处不在。发生这种情况时,除了这段代码之外,没有人正在读/写领域。

有谁知道为什么会这样吗?

如果我不清楚,请告诉我,我会尽力更清楚地解释。

最佳答案

这是预期的行为。为了实现零拷贝存储系统,Realm根本不保存实际数据。 Realm对持久属性的属性访问器进行调整以动态获取属性,因此,当您多次访问该属性时,实际上,根据设计,每次访问它实际上都会返回一个不同的实例。 RLMArray也与此相同。因此,每次您要访问RLMArray的元素时,Realm都会通过创建一个不同的代理对象来返回。

仅供参考:如果对象不是持久性的,RLMArray每次都会返回相同的实例。因为RLMArray仅由NSArray https://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

关于ios - 每次RLMArray <>返回不同的实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32303524/

10-13 03:47