Skip to content

Commit

Permalink
Merge 186f651 into 2f77fdf
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Feb 22, 2022
2 parents 2f77fdf + 186f651 commit 04a6e2b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
8 changes: 6 additions & 2 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,13 @@ 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);
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
9 changes: 5 additions & 4 deletions lib/src/realm_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,18 @@ class RealmObject {
return true;
}

Realm? get realm => _realm;

bool get isManaged => _realm != null;

/// `true` if this `RealmObject` is equal to another `RealmObject`.
@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 @@ -267,9 +271,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
33 changes: 30 additions & 3 deletions test/realm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Future<void> main([List<String>? args]) async {
path = _path.join(Configuration.filesPath, path);
}
Configuration.defaultPath = path;

addTearDown(() async {
var file = File(path);
if (await file.exists() && file.path.endsWith(".realm")) {
Expand Down Expand Up @@ -212,7 +212,7 @@ Future<void> main([List<String>? args]) async {
Configuration config = Configuration([Car.schema]);
var realm = Realm(config);
realm.close();

// Open an existing realm as readonly.
config = Configuration([Car.schema], readOnly: true);
realm = Realm(config);
Expand Down Expand Up @@ -1510,7 +1510,7 @@ Future<void> main([List<String>? args]) async {
realm.close();
});

test('Equals', () {
test('RealmObject.operator==', () {
var config = Configuration([Dog.schema, Person.schema]);
var realm = Realm(config);

Expand Down Expand Up @@ -1737,5 +1737,32 @@ 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 04a6e2b

Please sign in to comment.