Closed RileyGB closed 2 months ago
➤ PM Bot commented:
Jira ticket: RKOTLIN-1063
There is a difference between removing an object from a list and delete the object: https://www.mongodb.com/docs/atlas/device-sdks/sdk/kotlin/realm-database/crud/delete/#remove-elements-from-a-realmlist
There is a difference between removing an object from a list and delete the object: https://www.mongodb.com/docs/atlas/device-sdks/sdk/kotlin/realm-database/crud/delete/#remove-elements-from-a-realmlist
Hi Kneth, thanks for the reply!
Are you referencing this?
Realm collection instances that contain objects only store references to those objects. You can remove one or more referenced objects from a collection without deleting the objects themselves. The objects that you remove from a collection remain in the realm until you manually delete them. Alternatively, deleting a Realm object from a realm also deletes that object from any collection instances that contain the object.
If it is intentional that .removeAt
deletes the object, but .remove
does not, I would find that to be pretty unintuitive. Every other list in Kotlin removes an item from a list when .remove
is called (so long as a the item exists within the list).
The docs are pretty unclear or wrong here if you look at the examples provided below:
To remove one element from the list, pass the element to list.remove().
// Remove the first pond in the list
val removeFirstPond = forestPonds.first()
forestPonds.remove(removeFirstPond)
assertEquals(4, forestPonds.size)
According to my example above, this assertion would fail. Presumably because RealmInterop.realm_list_erase
is not called via RealmList.remove
as it is not overridden in ManagedRealmList.
Any changes for this issue?
Hi @c-villain. I think the issue here is that you are actually looking up the child
in an outdated context.
With
realm.writeBlocking {
val parent = realm.query<ParentModel>("id = $0", "1").first().find()!!
val child = parent.children.first()
val removedChild = findLatest(parent)!!.children.remove(child)
assertThat(removedChild, `is`(true)) // returns false <--- remove(RealmObject) does not work as expected, this is the problem
}
The instances parent
and child
are queried from your realm
instead of the MutableRealm
-receiver of the block
argument or writeBlocking
. This will try to remove an outdated (or not most recent) version of the object from the list. This will not match and hence will not be removed.
To achieve your intent you will have to either ensure that you are removing an up-to-date reference with:
val removedChild = findLatest(parent)!!.children.remove(findLatests(child)!!) // Added findLatest around child
Alternatively you could just operate solely on objects from the MutableRealm
with
realm.writeBlocking { // this: MutableRealm ->
// using this (MutableRealm) instead of global frozen `realm`
val parent = this.query<ParentModel>("id = $0", "1").first().find()!!
val child = parent.children.first()
val removedChild = parent.children.remove(child)
assertThat(removedChild, `is`(true))
}
I have created #1723 to improve the APIs and/or throw in these case, so will close this issue for now. If the advised details in https://github.com/realm/realm-kotlin/issues/1712#issuecomment-2058989283 is not fixing your issue, please leave a note and we can reinvestigate.
How frequently does the bug occur?
Always
Description
If I have two simple RealmObjects, where one is the parent and the other is a list of child objects within the parent, when I call
parent.children.remove(child)
, the child is not removed from the RealmList.It appears that
Collections.removeAt
is overridden in ManagedRealmList, whereasCollections.remove
is not.Stacktrace & log output
No response
Can you reproduce the bug?
Yes
Reproduction Steps
Here's models and tests that demonstrate the issue:
Models
Tests
Note:
RealmInstrumentedTest
is internal to my project, it basically just opens Realm with ourRealmConfiguration
and initializesmyTestRealm
.Of note here is that the assertion in
Test RealmList object remove
fails.Version
1.8.0
What Atlas App Services are you using?
Local Database only
Are you using encryption?
Yes
Platform OS and version(s)
All
Build environment
Android Studio version: Iguana 2023.2.1 Android Build Tools version: 7.4.2 Gradle version: 7.5