Realm opens twice at login. I have a kmm app. On ...
# multiplatform
Realm opens twice at login. I have a kmm app. On Swift, when I do the login, somehow the Realm opens twice. My login view:
Copy code
struct LoginView: View {

    @State var userId : String = ""
    @State var password : String = ""
    @State var myUser = UserInfo()
    var repo = RealmRepo()

    var body: some View {


                TextField("Username", text: $userId)
                TextField("Password", text: $password)
                    repo.login(email: $userId.wrappedValue, password: $password.wrappedValue) { user, error in

                        if(user != nil){
                            self.repo.getUserProfile().watch(block:  {items in
                                self.myUser = items as! UserInfo

I do the login, and if the login is ok, I get the profile of the user. My realm repo:
Copy code
suspend fun login(email: String, password: String): User {
        return appService.login(Credentials.emailPassword(email, password))

fun getUserProfile(): CommonFlow<UserInfo?> {
        val userId = appService.currentUser!!.id

        val user = realm.query<UserInfo>("_id = $0", userId).asFlow().map {

        return user

 private val appService by lazy {
        val appConfiguration =
            AppConfiguration.Builder(appId = "xxxx").log(LogLevel.ALL).build()

 private val realm by lazy {
        val user = appService.currentUser!!

        val config =
            SyncConfiguration.Builder(user, schemaClass).name("xxxx").schemaVersion(1)
                .initialSubscriptions { realm ->
                    add(realm.query<UserInfo>(), name = "user info", updateExisting = true)

When I do the login, will print 2 times the log:
INFO: [REALM] Realm opened: /var/mobile/
How is this possible? Is it because the the login block? Should I make the login and getUserProfile calls in a different way?
I had a similar problem where Android would work fine but iOS would open a service multiple times. I was using Koin to
services. I eventually found that when if the service was first accessed from from different Default threads, it would create a new instance of the service. I overcame the problem by ensuring the service was accessed on the Main thead.