diff --git a/Realm/RLMSwiftCollectionBase.mm b/Realm/RLMSwiftCollectionBase.mm index 9e857741a6..49743a3d6a 100644 --- a/Realm/RLMSwiftCollectionBase.mm +++ b/Realm/RLMSwiftCollectionBase.mm @@ -85,10 +85,14 @@ - (id)objectForKeyedSubscript:(id)key { } - (BOOL)isEqual:(id)object { - if (auto base = RLMDynamicCast(object)) { - return !base._rlmCollection.realm - && ((self._rlmCollection.count == 0 && base._rlmCollection.count == 0) || - [self._rlmCollection isEqual:base._rlmCollection]); + if (auto other = RLMDynamicCast(object)) { + if ((self == other) || (self._rlmCollection == other._rlmCollection)) { + return YES; + } + if (self._rlmCollection.realm != other._rlmCollection.realm) { + return NO; + } + return !self._rlmCollection.realm && [self._rlmCollection isEqual:other._rlmCollection]; } return NO; } diff --git a/RealmSwift/Tests/ListTests.swift b/RealmSwift/Tests/ListTests.swift index 66f6f2160d..904f87d2a5 100644 --- a/RealmSwift/Tests/ListTests.swift +++ b/RealmSwift/Tests/ListTests.swift @@ -601,6 +601,38 @@ class ListTests: TestCase { list.realm?.cancelWrite() } + + func testListEquality() { + let list1 = List() + list1.append(objectsIn: [1, 2, 3]) + XCTAssertEqual(list1, list1) + + let list2 = List() + list2.append(objectsIn: [1, 2, 3]) + XCTAssertEqual(list1, list2) + + let realm = try! Realm() + + let listObj = SwiftListObject(value: ["int": list1]) + + try! realm.write { + realm.add(listObj) + } + + XCTAssertEqual(listObj.int, listObj.int) + XCTAssertNotEqual(list1, listObj.int) + XCTAssertEqual(list1, list2) + + let listObj2 = SwiftListObject(value: ["int": list2]) + + try! realm.write { + realm.add(listObj2) + } + + XCTAssertEqual(listObj2.int, listObj2.int) + XCTAssertNotEqual(listObj2.int, list2) + XCTAssertEqual(list1, list2) + } func testUnmanagedListComparison() { let obj = SwiftIntObject()