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

kn/RealmSet.asResults #1214

Merged
merged 4 commits into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Deprecated `SyncResolveError` and `SyncResolveErrorCode` ([#1182](https://github.com/realm/realm-dart/pull/1182)).
* Added `SyncWebSocketError` and `SyncWebSocketErrorCode` for web socket connection sync errors ([#1182](https://github.com/realm/realm-dart/pull/1182)).
* Added `FlexibleSyncConfiguration.shouldCompactCallback` support ([#1204](https://github.com/realm/realm-dart/pull/1204)).
* Added `RealmSet.asResults()` ([#1214](https://github.com/realm/realm-dart/pull/1214)).

### Fixed
* You may have a crash on Windows if you try to open a file with non-ASCII path (Core upgrade).
Expand Down
5 changes: 5 additions & 0 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,11 @@ class _RealmCore {
return RealmResultsHandle._(pointer, list.realm.handle);
}

RealmResultsHandle resultsFromSet(RealmSet set) {
final pointer = _realmLib.invokeGetPointer(() => _realmLib.realm_set_to_results(set.handle._pointer));
return RealmResultsHandle._(pointer, set.realm.handle);
}

Object? resultsGetElementAt(RealmResults results, int index) {
return using((Arena arena) {
final realm_value = arena<realm_value_t>();
Expand Down
10 changes: 10 additions & 0 deletions lib/src/set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import 'native/realm_core.dart';
import 'realm_class.dart';
import 'realm_object.dart';
import 'collections.dart';
import 'results.dart';

/// RealmSet is a collection that contains no duplicate elements.
abstract class RealmSet<T extends Object?> extends SetBase<T> with RealmEntity implements Finalizable {
Expand Down Expand Up @@ -95,6 +96,9 @@ abstract class RealmSet<T extends Object?> extends SetBase<T> with RealmEntity i
/// ```
@override
bool remove(covariant T value); //Note: explicitly overriding remove() to change parameter type

/// Converts this [Set] to a [RealmResults].
RealmResults<T> asResults();
}

class UnmanagedRealmSet<T extends Object?> extends collection.DelegatingSet<T> with RealmEntity implements RealmSet<T> {
Expand All @@ -111,6 +115,9 @@ class UnmanagedRealmSet<T extends Object?> extends collection.DelegatingSet<T> w

@override
Stream<RealmSetChanges<T>> get changes => throw RealmStateError("Unmanaged RealmSets don't support changes");

@override
RealmResults<T> asResults() => throw RealmStateError("Unmanaged sets can't be converted to results");
}

class ManagedRealmSet<T extends Object?> with RealmEntity, SetMixin<T> implements RealmSet<T> {
Expand Down Expand Up @@ -248,6 +255,9 @@ class ManagedRealmSet<T extends Object?> with RealmEntity, SetMixin<T> implement

return true;
}

@override
RealmResults<T> asResults() => RealmResultsInternal.create<T>(realmCore.resultsFromSet(this), realm, _metadata);
}

/// @nodoc
Expand Down
18 changes: 16 additions & 2 deletions test/realm_set_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
//
////////////////////////////////////////////////////////////////////////////////

import 'dart:typed_data';

import 'package:test/test.dart' hide test, throws;
import '../lib/realm.dart';

Expand Down Expand Up @@ -645,4 +643,20 @@ Future<void> main([List<String>? args]) async {
expect(testSet.objectsSet.length, 2);
expect(realm.all<Car>().length, 2);
});

test('RealmSet.asResults()', () {
var config = Configuration.local([TestRealmSets.schema, Car.schema]);
var realm = getRealm(config);

final cars = [Car("Tesla"), Car("Audi")];
final testSets = TestRealmSets(1)..objectsSet.addAll(cars);

expect(() => testSets.objectsSet.asResults(), throwsStateError); // unmanaged set

realm.write(() {
realm.add(testSets);
});

expect(testSets.objectsSet.asResults(), cars);
});
}