How to not get different memory addresses for elements in a Realm List?

巧了我就是萌 提交于 2020-01-14 03:40:12

问题


If I iterate over an array in Swift with:

for element in array {
    ...
}

and place a breakpoint inside the for statement and look at the address of the object stored as element, than I get a different address than if I let the debugger run the expression:

array.first

There is only one element in the array. So, why do I not get the same address? Here is a screenshot of the debugger in action:

I am iterating over the attachments of a mail. When I debugged, the mail only had one attachment. The debugger also stopped in the first iteration. In my two expressions you can see, that attachment has another address than attachments.first

I don't know if that helps or matters, but Message is an object managed by Realm and so is Attachment. message.attachments is of type List.

EDIT:

I investigated further and it gets more weird. I have a class Message, which is a Realm Object and has a property List of Attachments, which are also Realm Objects.

import RealmSwift
class Message: Object {
    let attachments = List<Attachment>()
    ...
}
class Attachment: Object {
    ...
}

I debugged the point where attachments are appended. After all attachments are processed, I can ask for the first element and always get the same instance with the same memory address. Behavior is like I expect it.

When the message in later usage is passed around views, I can see, that the address of the message is the same in every view holding a reference. But if I then ask for the first attachment, I always get a different address. I even get different addresses when not moving in debug mode and just running the same expression multiple times:

This is not like I expect it.


回答1:


Realm Lists aren't Swift Arrays. The objects in a List are actually wrappers around a pointer to the location of the logical object in the database, and are created anew each time an element of a List is retrieved.

Realm does not make any guarantees about whether Realm object instances sourced from our object-retrieval APIs are going to be equal on a pointer-comparison basis; any semantic guarantees we do make all have to do with the identity of the objects within the database itself. (A single given object in the database may be represented by many Object instances, each of which might be a different Swift object but all of which point to the same underlying Realm object.)




回答2:


Although I do not fully understand yet, why two weeks ago everything worked as intended without the need of overwriting functions, making use of the Realm function isSameObject helped me fix my problem.

class Attachment: Object {

    override func isEqual(_ object: Any?) -> Bool {
        return self.isSameObject(as: object as? Object)
    }
}


来源:https://stackoverflow.com/questions/47775273/how-to-not-get-different-memory-addresses-for-elements-in-a-realm-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!