Comparing Realm Object List

Comparing Objects Realm Swift

Realm objects already conform to Equatable and Hashable since they are subclasses of NSObject. The only thing you need to do is override the isEqual method:

import UIKit
import RealmSwift

class ObjectClass: Object {

@objc dynamic var order = 0
@objc dynamic var number = ""
@objc dynamic var temp = 0
@objc dynamic var state = 1

override func isEqual(_ object: Any?) -> Bool {
if let object = object as? ObjectClass {
return self.order == object.order && self.number == object.number
&& self.temp == object.temp && self.state == object.state
} else {
return false
}
}
}

Compare realm objects

isEqualToObject: checks if two object instances refer to the same underlying persisted object.

Comparing the string data in the array and in the Realm database?

If you want to fetch an EventsDB object from your Realm whose dataMonth property is set to a certain month, you can use below piece of code:

let month = "April"
let events = realm.objects(EventsDB.self)
let eventsInApril = events.filter("dataMonth == %@",month)

Testing for equality in Realm

Katsumi from Realm here. Realm object's Equatable is implemented as follows:

BOOL RLMObjectBaseAreEqual(RLMObjectBase *o1, RLMObjectBase *o2) {
// if not the correct types throw
if ((o1 && ![o1 isKindOfClass:RLMObjectBase.class]) || (o2 && ![o2 isKindOfClass:RLMObjectBase.class])) {
@throw RLMException(@"Can only compare objects of class RLMObjectBase");
}
// if identical object (or both are nil)
if (o1 == o2) {
return YES;
}
// if one is nil
if (o1 == nil || o2 == nil) {
return NO;
}
// if not in realm or differing realms
if (o1->_realm == nil || o1->_realm != o2->_realm) {
return NO;
}
// if either are detached
if (!o1->_row.is_attached() || !o2->_row.is_attached()) {
return NO;
}
// if table and index are the same
return o1->_row.get_table() == o2->_row.get_table()
&& o1->_row.get_index() == o2->_row.get_index();
}

In summary, a) if both objects are unmanaged, it works same as normal object's Equatable. b) if both objects are managed, if they are the same table (class) and index, they are equal. c) If one is managed, another is unmanaged, ther are not equal.

"managed" means the object has stored in Realm.

So a and b in your code is not equal. Because a and b are unmanaged (have not stored in Realm) and they are different objects.

let a = Blurb()
a.text = "asdf"
let b = Blurb()
b.text = "asdf"
XCTAssertEqual(a.text, b.text)

Furthermore, when testing the equality, Realm doesn't care the values of the objects. Realm checks only a table and row index (as mentioned "b)"). Because different objects that has the same value are stored in the database is normal.

An example that two objects are equal is like the following:

let a = Blurb()
a.text = "asdf"

let realm = try! Realm()
try! realm.write {
realm.add(a)
}

let b = realm.objects(Blurb.self).first!
print(a == b) // true

How to compare two fields in android realm.io

Christian from Realm here. It is the second option. That query will compare the field "firstField" to the string "secondField". Unfortunately there is no easy way to do what you want, but it is probably not a uncommon use case so it is something we will take a look at how to support more easily.

Currently you would have to do it by hand:

RealmResults<Something> r = realm.where(Something.class)
.notEqualTo("firstField","secondField")
.findAll();

for (Something obj : r) {
if (!obj.getFirstField().equals(obj.getSecondField())) {
// Handle object
}
}


Related Topics



Leave a reply



Submit