Skip to content

Commit

Permalink
Adding insertToRealm: a fast and more memory efficient way to copy da…
Browse files Browse the repository at this point in the history
…ta into Realm
  • Loading branch information
nhachicha committed Aug 7, 2024
1 parent d81500c commit d88f4e6
Show file tree
Hide file tree
Showing 6 changed files with 1,817 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public interface MutableRealm : TypedRealm {
*/
public fun <T : RealmObject> copyToRealm(instance: T, updatePolicy: UpdatePolicy = UpdatePolicy.ERROR): T

public fun <T : RealmObject> insertToRealm(instance: T, updatePolicy: UpdatePolicy = UpdatePolicy.ERROR)

public fun <T : RealmObject> insertToRealm(instances: Collection<T>, updatePolicy: UpdatePolicy = UpdatePolicy.ERROR)

/**
* Returns a [RealmQuery] matching the predicate represented by [query].
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ internal interface InternalMutableRealm : MutableRealm {
return copyToRealm(configuration.mediator, realmReference, instance, updatePolicy)
}

override fun <T : RealmObject> insertToRealm(instance: T, updatePolicy: UpdatePolicy) {
val cache: UnmanagedToManagedObjectPointerCache = mutableMapOf()

insertToRealm(realmReference, instance, updatePolicy, cache)

cache.forEach { it.value.release() }
cache.clear()
}

override fun <T : RealmObject> insertToRealm(instances: Collection<T>, updatePolicy: UpdatePolicy) {
val cache: UnmanagedToManagedObjectPointerCache = mutableMapOf()

insertToRealm(realmReference, instances, updatePolicy, cache)

cache.forEach { it.value.release() }
cache.clear()
}

override fun delete(deleteable: Deleteable) {
deleteable.asInternalDeleteable().delete()
}
Expand Down
Loading

0 comments on commit d88f4e6

Please sign in to comment.