Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return from write #294

Merged
merged 14 commits into from
Mar 25, 2022
Merged
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
==============================================================

Expand Down
9 changes: 5 additions & 4 deletions lib/src/realm_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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>(T Function() writeCallback) {
try {
realmCore.beginWrite(this);
writeCallback();
T result = writeCallback();
realmCore.commitWrite(this);
return result;
} catch (e) {
if (_isInTransaction) {
realmCore.rollbackWrite(this);
Expand Down Expand Up @@ -234,7 +235,7 @@ class Realm {
}

/// Deletes all [RealmObject]s of type `T` in the `Realm`
void deleteAll<T extends RealmObject>() => deleteMany(all<T>());
void deleteAll<T extends RealmObject>() => deleteMany(all<T>());
}

class Scheduler {
Expand Down Expand Up @@ -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);

Expand Down
14 changes: 14 additions & 0 deletions test/realm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -595,4 +595,18 @@ 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 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();
});

}