This is a question about the inner workings of Realm and the Kotlin Random number generator. When I incorrectly had several Realm transaction instances open on a background thread (as many as 100 by accident), I observed that setting a Realm object field value to kotlin.Random.nextInt() was causing collisions over 20% of the time, which should not happen.
What about Realm (or memory in general) could cause a side effect like this on kotlin.Random? Intuitively, a random number generator should not be affected by the usage of our local database. Would this also affect behavior like UUID.randomUUID()?
Also, I'm aware the Realm should be closed - I'm genuinely curious why this is happening.
open class MyObject: RealmObject {
var randomField: Int
}
fun test() = scope.launch { // background thread
var messageDupes = 0
var r = Realm.getInstance()
while (idx < 100) {
r = Realm.getInstance() // opening of Realm without closing
val random = Random.nextInt() // Why would this be affected? Has many collisions
val existing = r.where<MyObject>.equalTo("randomField", random)
if (existing != null) {
collisions++
}
val obj = MyObject()
obj.randomField = random //set field to the random value
r.executeTransaction {
it.insert(message)
}
idx++
delay(300L)
}
val count = r.where<MyObject>().count()
log.debug("total objects: $count; collisions=$collisions")
}
Aucun commentaire:
Enregistrer un commentaire