realm-kotlin icon indicating copy to clipboard operation
realm-kotlin copied to clipboard

Add support for RealmSet

Open edualonso opened this issue 3 years ago • 0 comments

Follow the pattern of managed/unmanaged used for lists by using delegates.

An oversimplified API would look like this:

interface RealmSet<E> : MutableSet<E>

internal class UnmanagedRealmSet<E>: RealmSet<E>,
    MutableSet<E> by mutableSetOf()

internal class ManagedRealmSet<E>(
    val nativePointer: NativePointer
): AbstractMutableSet<E>(), RealmSet<E> {
    // TODO
}

/**
 * Instantiates an **unmanaged** [RealmSet].
 */
fun <E> realmSetOf(vararg elements: E): RealmSet<E> = UnmanagedRealmSet()

/**
 * Instantiates an **unmanaged** [RealmSet] containing all the entries of this set.
 */
fun <E> Set<E>.toRealmSet(): RealmSet<E> = TODO()

Observations:

  • the C-API has defined the methods for sets but is still missing their implementations.
  • the majority of methods from the MutableSet interface are already provided by AbstractMutableSet though they might not be optimised, e.g. addAll iterates over all values in the provided collection and then calls add.
  • It doesn't seem necessary to rely on Mixed as the transport solution since the C-API accepts realm_value_t structs, just as RealmList does.

edualonso avatar Nov 05 '21 10:11 edualonso