Skip to content

Commit

Permalink
Merge 800b955 into 471e2d8
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Feb 24, 2022
2 parents 471e2d8 + 800b955 commit 6fe670d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
11 changes: 7 additions & 4 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -516,18 +516,21 @@ class _RealmCore {
_realmLib.invokeGetBool(() => _realmLib.realm_list_clear(list.handle._pointer));
}

bool equals(RealmObject first, RealmObject second) {
return _realmLib.realm_equals(first.handle._pointer.cast(), second.handle._pointer.cast());
bool _equals<T extends NativeType>(Handle<T> first, Handle<T> second) {
return _realmLib.realm_equals(first._pointer.cast(), second._pointer.cast());
}

bool objectEquals(RealmObject first, RealmObject second) => _equals(first.handle, second.handle);
bool realmEquals(Realm first, Realm second) => _equals(first.handle, second.handle);
bool configurationEquals(Configuration first, Configuration second) => _equals(first.handle, second.handle);

bool objectIsValid(RealmObject object) {
return _realmLib.realm_object_is_valid(object.handle._pointer);
}

bool listIsValid(RealmList list) {
return _realmLib.realm_list_is_valid(list.handle._pointer);
}

static void collection_change_callback(Object object, Pointer<realm_collection_changes> data) {
assert(object is NotificationsController, "Notification controller expected");

Expand Down
12 changes: 12 additions & 0 deletions lib/src/realm_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ class Realm {
final handle = realmCore.queryClass(this, metadata.class_.key, query, args);
return RealmResultsInternal.create<T>(handle, this);
}

@override
// ignore: hash_and_equals
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! Realm) return false;
bool areEquals = realmCore.realmEquals(this, other);
if (!areEquals) {
areEquals = realmCore.configurationEquals(config, other.config);
}
return areEquals;
}
}

class Scheduler {
Expand Down
10 changes: 5 additions & 5 deletions lib/src/realm_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,17 @@ class RealmObject {
return true;
}

/// `true` if this `RealmObject` is equal to another `RealmObject`.
Realm? get realm => _realm;

bool get isManaged => _realm != null;

@override
// ignore: hash_and_equals
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! RealmObject) return false;
if (!isManaged || !other.isManaged) return false;
return realmCore.equals(this, other);
return realmCore.objectEquals(this, other);
}

/// Gets a value indicating whether this object is managed and represents a row in the database.
Expand Down Expand Up @@ -295,9 +298,6 @@ extension RealmObjectInternal on RealmObject {

RealmObjectHandle get handle => _handle!;
RealmAccessor get accessor => _accessor;
Realm? get realm => _realm;

bool get isManaged => _realm != null;
}

/// An exception being thrown when a `Realm` operation or [RealmObject] access fails.
Expand Down
27 changes: 27 additions & 0 deletions test/realm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,31 @@ Future<void> main([List<String>? args]) async {
expect(mainSchools[0].branches[0].students.length + mainSchools[0].branches[1].students.length, 3);
realm.close();
});

test('Realm.operator== ', () {
final config = Configuration([Dog.schema, Person.schema]);
final r1 = Realm(config);
expect(r1, r1);
r1.close();
});

test('Realm.operator== same config', () {
final config = Configuration([Dog.schema, Person.schema]);
final r1 = Realm(config);
final r2 = Realm(config);

expect(r1, r2);
r1.close();
r2.close();
});

test('Realm.operator== different config', () {
var config = Configuration([Dog.schema, Person.schema]);
final r1 = Realm(config);
config = Configuration([Dog.schema, Person.schema]);
final r2 = Realm(config);
expect(r1, isNot(r2));
r1.close();
r2.close();
});
}

0 comments on commit 6fe670d

Please sign in to comment.