比较领域对象列表 [英] Comparing Realm Object List

查看:81
本文介绍了比较领域对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将2个Realm对象列表相互比较,以查看它们是否相同. 这是该对象的外观.

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 = ""   
}

当我打印2个列表时:

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;
}
)

我尝试使用'=='或'isEqual',但是即使列表相同,它仍然会导致错误. 我该怎么做,看看它们是否彼此相同.

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.

推荐答案

Realm覆盖==函数,请参见 Realm对象的Equatable是实现.

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

简而言之,您所比较的两个列表可能被Realm视为不相等,因为它们都不是Realm中的托管列表,或者不是存储在不同的Realms中.在这种情况下,您可以通过制作托管列表的非托管副本并将其与其他列表进行比较来避免这种情况.

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.

这篇关于比较领域对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆