Skip to content

Commit

Permalink
realm delete many (#153)
Browse files Browse the repository at this point in the history
Implemented realm.deleteMany for List, RealmList and realmResults to delete objects from realm - fixes #60
Support RealmResults for..in  - fixes #37 without snapshot
  • Loading branch information
desistefanova authored Jan 11, 2022
1 parent 38f5667 commit 4b89c39
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,6 @@
"flutter/realm_flutter/ios/src/**": true,
"flutter/realm_flutter/lib/**": true,
"flutter/realm_flutter/test/**": true
}
},
"dart.lineLength": 160
}
11 changes: 9 additions & 2 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class _RealmCore {
});
}

void removeRealmObject(RealmObject object) {
void deleteRealmObject(RealmObject object) {
_realmLib.invokeGetBool(() => _realmLib.realm_object_delete(object.handle._pointer));
}

Expand Down Expand Up @@ -424,10 +424,17 @@ class _RealmCore {
});
}

void listDeleteAll(RealmList list) {
_realmLib.invokeGetBool(() => _realmLib.realm_list_remove_all(list.handle._pointer));
}

void resultsDeleteAll(RealmResults results) {
_realmLib.invokeGetBool(() => _realmLib.realm_results_delete_all(results.handle._pointer));
}

void listClear(RealmList list) {
_realmLib.invokeGetBool(() => _realmLib.realm_list_clear(list.handle._pointer));
}

}

class LastError {
Expand Down
25 changes: 24 additions & 1 deletion lib/src/realm_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,37 @@ class Realm {
return object;
}

/// Delete given [RealmObject] from Realm database.
/// Throws [RealmException] on error.
void delete<T extends RealmObject>(T object) {
try {
realmCore.deleteRealmObject(object);
} catch (e) {
throw RealmException("Error deleting object from databse. Error: $e");
}
}

/// Deletes [RealmObject] items in given collection from Realm database.
void deleteMany<T extends RealmObject>(Iterable<T> items) {
if (items is RealmResults<T>) {
realmCore.resultsDeleteAll(items);
} else if (items is RealmList<T>) {
realmCore.listDeleteAll(items);
} else {
for (T realmObject in items) {
realmCore.deleteRealmObject(realmObject);
}
}
}

void addAll<T extends RealmObject>(Iterable<T> items) {
for (final i in items) {
add(i);
}
}

void remove<T extends RealmObject>(T object) {
realmCore.removeRealmObject(object);
realmCore.deleteRealmObject(object);
}

bool get _isInTransaction => realmCore.getIsWritable(this);
Expand Down
37 changes: 33 additions & 4 deletions lib/src/results.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class _ResultsList<T extends RealmObject> extends collection.ListBase<T> {
/// Instances of this class are typically live collections returned by [Realm.objects]
/// that will update as new objects are either added to or deleted from the Realm
/// that match the underlying query.
class RealmResults<T extends RealmObject> {
class RealmResults<T extends RealmObject> extends collection.IterableBase<T> {
late final RealmResultsHandle _handle;
late final Realm _realm;
// RealmResults _results;
Expand Down Expand Up @@ -143,16 +143,19 @@ class RealmResults<T extends RealmObject> {
// }

/// Returns `true` if the Results collection is empty
bool isEmpty() {
return length == 0;
}
@override
bool get isEmpty => length == 0;

@override
Iterator<T> get iterator => _RealmResultsIterator(this);

/// Returns `true` if this Results collection has not been deleted and is part of a valid Realm.
///
/// Accessing an invalid Results collection will throw an [RealmException]
// bool get isValid => _results.isValid();

/// Returns the number of values in the Results collection.
@override
int get length => realmCore.getResultsCount(this);

/// Returns a human-readable description of the objects contained in the collection.
Expand Down Expand Up @@ -208,3 +211,29 @@ extension RealmResultsInternal on RealmResults {
return RealmResults<T>._(handle, realm);
}
}

class _RealmResultsIterator<T extends RealmObject> implements Iterator<T> {
final RealmResults<T> _results;
int _index;
T? _current;

_RealmResultsIterator(RealmResults<T> results)
: _results = results,
_index = -1;

@override
T get current => _current as T;

@override
bool moveNext() {
int length = _results.length;
_index++;
if (_index >= length) {
_current = null;
return false;
}
_current = _results[_index];

return true;
}
}
Loading

0 comments on commit 4b89c39

Please sign in to comment.