Skip to content

Commit

Permalink
Return result from write method callback. Fixes #67
Browse files Browse the repository at this point in the history
  • Loading branch information
desistefanova committed Mar 14, 2022
1 parent a38a346 commit 01e91e0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
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) {
TResult write<TResult>(TResult Function() writeCallback) {
try {
realmCore.beginWrite(this);
writeCallback();
TResult 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
13 changes: 13 additions & 0 deletions test/realm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ Future<void> main([List<String>? args]) async {
realm.close();
});

test('Realm return added object', () {
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();
});

test('Realm add multiple objects', () {
final config = Configuration([Car.schema]);
final realm = Realm(config);
Expand Down

0 comments on commit 01e91e0

Please sign in to comment.