Realm opens twice at login. I have a kmm app. On ...
# realm
d
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 {

        NavigationView{
            VStack(){

                TextField("Username", text: $userId)
                        
                TextField("Password", text: $password)
                        
                Button("Login"){
  
                    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 {
            it.list.firstOrNull()
        }.asCommonFlow()

        return user
    }

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

 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)

                }.waitForInitialRemoteData().build()
        Realm.open(config)
    }
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?
c
The library opens multiple instances of the realm under the hood. One main instance that is the one you query from
realm.query
and two live instances - one where all the writes are happening and one that monitors objects for notifications. So you can see up to three log statements like the above when you call
Realm.open
The _writer-_instance is only opened if you actually
write
something, so that it why you might only see two.
d
By multiple instances of the realm under the hood you mean
Realm Opened
?
On my main app I am getting somewhere from 3-4 Realm Opened, will that affect the performance of my app? Wouldn't this use my the db connections? I am using a M10 at the moment
c
If you get more than three
Realm opened
log statements with the same path then you are calling
Realm.open(configuration)
more than once. This is NOT recommended. It is recommend that you share the same instance across your application instead of opening it multiple times as each
Realm.open
will allocated additional resources (writer/notifier-threads, etc.). That said, if you open multiple instances it will still use the same sync session so shouldn't take additional server side resources.
d
You mean using a singleton repo, right?