I’m trying to fight large number of active realm v...
# realm
g
I’m trying to fight large number of active realm version after migration. I created this sample to try to understand it better. No matter what I do I cannot get the number down. I tried different realm versions
1.7.0
and latest
1.10.2
. When I remove the code to collect the flow and run GC manually it drops (but only in
1.7.0
). What am I missing here? What are the strategies that we can use today to keep active realm versions low?
Copy code
object Singleton {
    val config = RealmConfiguration.create(schema = setOf(Item::class))
    val realm = Realm.open(config)
}

class Item : RealmObject {
    @PrimaryKey
    var _id: ObjectId = ObjectId()
    var isComplete: Boolean = false
    var summary: String = ""
    var owner_id: String = ""
}

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val objectsCount = mutableStateOf(0L)
        val versionsCount = mutableStateOf(realm.getNumberOfActiveVersions())
        setContent {
            RealmTestTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Greeting(objectsCount.value.toString(), versionsCount.value.toString(), onClick = {
                        lifecycleScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
                            repeat(100) {
                                realm.write {
                                    copyToRealm(Item().apply {
                                        summary = "Do the laundry"
                                        isComplete = false
                                    })
                                }
                                delay(10)
                                versionsCount.value = realm.getNumberOfActiveVersions()
                            }
                        }
                    })
                }
            }
        }
        realm
            .query<Item>()
            .asFlow()
            .onEach {
                val summaries = it.list.map { it.summary }
                objectsCount.value = summaries.count().toLong()
            }
            .flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
            .launchIn(lifecycleScope)
    }
}

@Composable
fun Greeting(objectsCount: String, activeVersions: String, modifier: Modifier = Modifier, onClick: () -> Unit) {
        Column() {
            Text(
                text = "Objects $objectsCount",
                modifier = modifier
            )
            Text(
                text = "Active versions $activeVersions",
                modifier = modifier
            )
            Button(onClick = onClick) {
                Text(text = "Insert 100 items")
            }
        }
}
c
There was an error in how this number was recorded, so the number was just not right. We have merged a fix for it in https://github.com/realm/realm-kotlin/pull/1516 which should be available in the
1.11.2-SNAPSHOT
.
1