Daniel
03/09/2023, 6:22 PMstruct 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?Claus Rørbech
03/10/2023, 7:49 AMrealm.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.Daniel
03/10/2023, 12:36 PMRealm Opened
?Claus Rørbech
03/10/2023, 1:01 PMRealm 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.Daniel
03/10/2023, 6:06 PM