Len_X
Len_X

Reputation: 873

Comparing Realm Object List

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.

Upvotes: 2

Views: 2439

Answers (1)

David Pasztor
David Pasztor

Reputation: 54716

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.

Upvotes: 1

Related Questions