Comparing Realm Object List

放肆的年华 提交于 2019-12-18 09:37:58

问题


I want to compare 2 Realm object Lists with each other to see if they are identical. Here is what the object looks like.

class ScheduleRealm: Object {
let scheduleList = List<Schedule>()
}

class Schedule: Object {
dynamic var startTime : Date = Date()
dynamic var endTime : Date = Date()
dynamic var name : String = ""   
}

When I print the 2 list:

List<Schedule> (
[0] Schedule {
    startTime = 2017-07-03 16:00:00 +0000;
    endTime = 2017-07-03 18:00:00 +0000;
    name = Weights   Mon, 3 Jul 16:00 120m;
},
[1] Schedule {
    startTime = 2017-07-04 16:00:00 +0000;
    endTime = 2017-07-04 17:30:00 +0000;
    name = Weights   Tue, 4 Jul 16:00 90m;
},
[2] Schedule {
    startTime = 2017-07-10 01:30:00 +0000;
    endTime = 2017-07-10 04:30:00 +0000;
    name = Weights   Mon, 10 Jul 01:30 180m;
}
)
List<Schedule> (
[0] Schedule {
    startTime = 2017-07-03 16:00:00 +0000;
    endTime = 2017-07-03 18:00:00 +0000;
    name = Weights   Mon, 3 Jul 16:00 120m;
},
[1] Schedule {
    startTime = 2017-07-04 16:00:00 +0000;
    endTime = 2017-07-04 17:30:00 +0000;
    name = Weights   Tue, 4 Jul 16:00 90m;
},
[2] Schedule {
    startTime = 2017-07-10 01:30:00 +0000;
    endTime = 2017-07-10 04:30:00 +0000;
    name = Weights   Mon, 10 Jul 01:30 180m;
}
)

I have tried using '==' or 'isEqual', but even though the lists are identical it still results to a false. What can I do to see if they are identical to one another.


回答1:


Realm overrides the == function, see Realm object's Equatable is implementation.

In short, the two lists you are comparing might be considered not to be equal by Realm, because not both of them are managed lists in Realm or because they are stored in different Realms. If this is the case, you can circumvent this by making an unmanaged copy of the managed List and comparing that to the other list.



来源:https://stackoverflow.com/questions/45165526/comparing-realm-object-list

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