Daniel
03/09/2023, 6:18 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?Landry Norris
03/09/2023, 6:23 PMLandry Norris
03/09/2023, 6:24 PMDaniel
03/09/2023, 6:34 PMLandry Norris
03/09/2023, 6:35 PMLandry Norris
03/09/2023, 6:36 PMDaniel
03/09/2023, 6:36 PMDaniel
03/09/2023, 6:37 PMApp.create(appConfiguration)
Landry Norris
03/09/2023, 6:38 PMDaniel
03/09/2023, 6:38 PMLandry Norris
03/09/2023, 6:40 PMDaniel
03/09/2023, 6:47 PMDaniel
03/09/2023, 6:47 PMLandry Norris
03/09/2023, 6:49 PMDaniel
03/09/2023, 6:49 PMLandry Norris
03/09/2023, 6:50 PMDaniel
03/09/2023, 6:52 PMLandry Norris
03/09/2023, 6:54 PMLandry Norris
03/09/2023, 6:54 PMDaniel
03/09/2023, 6:57 PMDaniel
03/09/2023, 6:57 PMDaniel
03/09/2023, 6:57 PMLandry Norris
03/09/2023, 7:00 PMgetUserProfile()
emits a new value, you update self.myUser
, and SwiftUI sees that LoginView’s state has changed, and re-creates it. This is fine, since it’s expected that when you change the state, SwiftUI re-creates your LoginView so it can show any changes. In this case, however, you construct a new repo whenever LoginView gets constructed.Landry Norris
03/09/2023, 7:02 PMinsertFCMWhenLogin
modify the user profile at all, such that the watch would emit a new value?Daniel
03/09/2023, 7:04 PMLandry Norris
03/09/2023, 7:05 PMLandry Norris
03/09/2023, 7:06 PMDaniel
03/09/2023, 7:07 PM@State UserInfo
changes, the view will run again? With all the calls?Landry Norris
03/09/2023, 7:08 PMLandry Norris
03/09/2023, 7:09 PMLandry Norris
03/09/2023, 7:10 PMDaniel
03/09/2023, 7:11 PMDaniel
03/09/2023, 7:11 PMLandry Norris
03/09/2023, 7:12 PMDaniel
03/09/2023, 7:16 PMLandry Norris
03/09/2023, 7:17 PMLandry Norris
03/09/2023, 8:57 PMLandry Norris
03/09/2023, 8:57 PM