diff --git a/CHANGELOG.md b/CHANGELOG.md index b7ba5b7e1..79779b3cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +x.x.x Release notes (yyyy-MM-dd) +============================================================== + +**This project is in the Alpha stage. All API's might change without warning and no guarantees are given about stability. Do not use it in production.** + +### Enhancements +Support result value from write transaction callbacks ([#294](https://github.com/realm/realm-dart/pull/294/)) + + 0.2.1+alpha Release notes (2022-03-20) ============================================================== diff --git a/lib/src/realm_class.dart b/lib/src/realm_class.dart index 04c21ae09..fdbf89426 100644 --- a/lib/src/realm_class.dart +++ b/lib/src/realm_class.dart @@ -166,11 +166,12 @@ class Realm { /// /// If no exception is thrown from within the callback, the transaction will be committed. /// It is more efficient to update several properties or even create multiple objects in a single write transaction. - void write(void Function() writeCallback) { + T write(T Function() writeCallback) { try { realmCore.beginWrite(this); - writeCallback(); + T result = writeCallback(); realmCore.commitWrite(this); + return result; } catch (e) { if (_isInTransaction) { realmCore.rollbackWrite(this); @@ -234,7 +235,7 @@ class Realm { } /// Deletes all [RealmObject]s of type `T` in the `Realm` - void deleteAll() => deleteMany(all()); + void deleteAll() => deleteMany(all()); } class Scheduler { @@ -270,7 +271,7 @@ class Scheduler { extension RealmInternal on Realm { RealmHandle get handle => _handle; Scheduler get scheduler => _scheduler; - + RealmObject createObject(Type type, RealmObjectHandle handle) { RealmMetadata metadata = _getMetadata(type); diff --git a/test/realm_test.dart b/test/realm_test.dart index 84bc66213..e50572c7b 100644 --- a/test/realm_test.dart +++ b/test/realm_test.dart @@ -595,4 +595,18 @@ Future main([List? args]) async { expect(mainSchools[0].branches[0].students.length + mainSchools[0].branches[1].students.length, 3); realm.close(); }); + + test('Realm write returns result', () { + var config = Configuration([Car.schema]); + var realm = Realm(config); + var car = Car('Mustang'); + + var returnedCar = realm.write(() { + return realm.add(car); + }); + expect(returnedCar, car); + + realm.close(); + }); + }