I have a kmm app.
On Swift, when I do the login, somehow the Realm opens twice.
My login view:
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:
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?