Closed tomassasovsky closed 9 months ago
Do you want to supply your own equals implemented or do you just want to have a built-in function that compares the contents of an object?
To elaborate a bit further. We do support equality comparison of managed realm objects. As long as two instances refer to the same realm object, as in they have the same primary key (implicit or explicit) and stem from the same version of the realm, then they will compare equal.
The implementation is:
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! RealmObjectBase) return false;
if (!isManaged || !other.isManaged) return false;
return realmCore.objectEquals(this, other);
}
So when you say you need equality comparison, I suspect you mean either:
@tomassasovsky Could you clarify more what your intent is?
Ah, I'm sorry I wasn't clear enough. I want to compare different instances of unmanaged objects. This is super important for unit tests, since right now I can't make sure my parsing functions are generating what they're supposed to.
Would comparing the unmanaged object by primary key be enough for your use-case?
Also, have you considered storing the object in a realm (ie. make it managed)? For a unit test it might be preferable to use an in-memory realm (Realm(Configuration.inMemory(...))
)
No, that won't be enough because I want to make sure my models are parsing the data correctly, and that everything will work with the backend. So I do need the class fields to be considered in the comparison.
What's unclear to me is if it's a requirement that this works through the equals implementation or if you can use another function. Because if it's the latter, you can just write a function yourself like:
bool areFoosEqual(Foo left, Foo right) {
return left.prop1 == right.prop2 &&
left.prop2 == right.prop2 &&
...
}
Unless you really need to be the implementation of the generated equals method, you can use it in unit tests or when validating the contents of unmanaged objects.
You can also write it as an extension on Foo, making it look like an instance method:
extension FooEquality on Foo {
bool deepEquals(Foo other) { ... }
}
How would that work with a field that's a List<$RealmObject>
?
The same way deep equality works for any object - you'd recursively invoke their deep equals:
if (this.list.length != other.list.length) {
return false;
}
for (var i = 0; i < this.list.length; i++) {
final thisElement = this.list[i];
final otherElement = other.list[i];
if (!thisElement.deepEquals(otherElement)) {
return false;
}
}
I'd love to have this as a built-in feature so that this ends up being generated equals
function inside the class, and deepEquals
as an extension DeepEqualsT on T
.
Would that be possible?
This is not something that we intend to do in the short/medium-term. While I agree it's a generally useful feature, it is orthogonal to the database/synchronization product we're developing. Our goal is to be compatible with 3rd party packages, but it's not feasible to integrate every functionality in a database package.
Problem
I want to identify whether two RealmObjects contain the same data but don't necessarily care about them being the same instance.
How important is this improvement for you?
Dealbreaker
Feature would mainly be used with
Local Database only