Colton Idle
09/20/2024, 9:10 PMval supabase = createSupabaseClient(
supabaseUrl = "",
supabaseKey = ""
) {
install(Auth)
println("SUPABASE INIT")
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
}
}
}Colton Idle
09/20/2024, 9:11 PMsetContent {
supabase
}
then it will print. It's as if createSupabaseClient is acting lazy... but it shouldn't be.Joffrey
09/20/2024, 9:11 PMcreateSupabaseClient?Colton Idle
09/20/2024, 9:12 PMinline fun createSupabaseClient(supabaseUrl: String, supabaseKey: String, builder: SupabaseClientBuilder.() -> Unit) = SupabaseClientBuilder(supabaseUrl, supabaseKey).apply(builder).build()Joffrey
09/20/2024, 9:13 PMval supabase declaration actually in the same file?Colton Idle
09/20/2024, 9:13 PMColton Idle
09/20/2024, 9:13 PMColton Idle
09/20/2024, 9:13 PMJoffrey
09/20/2024, 9:13 PMColton Idle
09/20/2024, 9:14 PMJoffrey
09/20/2024, 9:14 PMcreateSupabaseClient is not lazy, but the initialization of val supabase as a whole isColton Idle
09/20/2024, 9:14 PMJoffrey
09/20/2024, 9:15 PMval supabase, and reference that other unrelated variable instead from setContent you will probably see your print tooColton Idle
09/20/2024, 9:16 PMColton Idle
09/20/2024, 9:16 PMColton Idle
09/20/2024, 9:16 PMColton Idle
09/20/2024, 9:16 PMIs theYes. It is in the same filedeclaration actually in the same file? (edited)val supabase
Joffrey
09/20/2024, 9:17 PMval supabase declaration in the same file as your activity?Colton Idle
09/20/2024, 9:17 PMpackage com.example.supabase1
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import com.example.supabase1.ui.theme.Supabase1Theme
import io.github.jan.supabase.auth.Auth
import io.github.jan.supabase.createSupabaseClient
val supabase = createSupabaseClient(
supabaseUrl = "",
supabaseKey = ""
) {
install(Auth)
println("SUPABASE INIT")
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Supabase1Theme {
}
}
}
}Colton Idle
09/20/2024, 9:17 PMJoffrey
09/20/2024, 9:17 PMColton Idle
09/20/2024, 9:18 PMJoffrey
09/20/2024, 9:20 PMJoffrey
09/20/2024, 9:21 PMNameOfFileKt and the other is MainActivity. If nothing from NameOfFileKt is used (meaning no top-level property or function from your file), then it won't be initializedColton Idle
09/20/2024, 9:22 PMColton Idle
09/20/2024, 9:22 PMColton Idle
09/20/2024, 9:22 PMclass MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.e("XYZ", "")
setContent {
Supabase1Theme {
buildString
}
}
}
}
val supabase = createSupabaseClient(
supabaseUrl = "",
supabaseKey = ""
) {
install(Auth)
println("SUPABASE INIT")
}
val buildString = buildString {
println("buildstring INIT")
append("hello world")
}Colton Idle
09/20/2024, 9:23 PMJoffrey
09/20/2024, 9:23 PMColton Idle
09/20/2024, 9:24 PMColton Idle
09/20/2024, 9:24 PMColton Idle
09/20/2024, 9:24 PMclass MainActivity : ComponentActivity() {
is a different file technciallyColton Idle
09/20/2024, 9:25 PMJoffrey
09/20/2024, 9:26 PMJoffrey
09/20/2024, 9:27 PMColton Idle
09/20/2024, 9:27 PMJoffrey
09/20/2024, 9:27 PMColton Idle
09/20/2024, 9:27 PMColton Idle
09/20/2024, 9:27 PMColton Idle
09/20/2024, 9:27 PMColton Idle
09/20/2024, 9:28 PMpackage com.example.supabase1
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import com.example.supabase1.ui.theme.Supabase1Theme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Supabase1Theme {
}
}
}
}
and then another file Blah.kt
package com.example.supabase1.ui
import io.github.jan.supabase.auth.Auth
import io.github.jan.supabase.createSupabaseClient
val supabase = createSupabaseClient(
supabaseUrl = "",
supabaseKey = ""
) {
install(Auth)
println("SUPABASE INIT")
}
val built = buildString {
println("buildstring INIT")
append("hello world")
}
and in this case. it makes sense to me why built or supabase aren't init'dJoffrey
09/20/2024, 9:29 PMJoffrey
09/20/2024, 9:30 PMpackage com.example.supabase1
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import com.example.supabase1.ui.theme.Supabase1Theme
import io.github.jan.supabase.auth.Auth
import io.github.jan.supabase.createSupabaseClient
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Supabase1Theme {
}
}
}
}
object MyOtherClass {
val supabase = createSupabaseClient(
supabaseUrl = "",
supabaseKey = ""
) {
install(Auth)
println("SUPABASE INIT")
}
val built = buildString {
println("buildstring INIT")
append("hello world")
}
}
It's also the same, modulo the class names of courseColton Idle
09/20/2024, 9:30 PMpackage com.example.supabase1
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import com.example.supabase1.ui.theme.Supabase1Theme
import io.github.jan.supabase.auth.Auth
import io.github.jan.supabase.createSupabaseClient
val supabase = createSupabaseClient(
supabaseUrl = "",
supabaseKey = ""
) {
install(Auth)
println("SUPABASE INIT")
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Supabase1Theme {
}
}
}
}
If I have the above... is there any easy way to load the supabase variable containing class without calling it in MainActivity?Colton Idle
09/20/2024, 9:31 PMsupabase in my MainActivity codeColton Idle
09/20/2024, 9:32 PMJoffrey
09/20/2024, 9:32 PMsupabase variable, right?Colton Idle
09/20/2024, 9:33 PMColton Idle
09/20/2024, 9:33 PMJoffrey
09/20/2024, 9:34 PMJoffrey
09/20/2024, 9:36 PMsuspend client creation that would only finish when the session is loaded or something. (I really don't have domain knowledge, so I might be speaking nonsense)Jan
09/20/2024, 9:50 PMSoYea that makes sense, forgot that top level Kotlin declarations work like that.is not lazy, but the initialization ofcreateSupabaseClientas a whole isval supabase
I guess i just want the app to have the auth already loading for the MainActivityYou could really just do something simple like this:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Supabase1Theme {
var loaded by remember { mutableStateOf(false) }
LaunchedEffect(Unit) {
supabase.auth.awaitInitialization()
loaded = true
}
if (loaded) {
//App
} else {
CircularProgressIndicator()
}
}
}
}
}
Or use the sessionStatus state flow as described in the other thread. What is to note here that initialization/loading doesn't only mean to read the session data from the FS. The data gets read and then maybe the session has to be refreshed (API call) and only after that the session is available in the SupabaseClient. The refresh operation can also fail due to e.g. network problems, so you have to also handle that. (Everything reflected in the sessionStatus API)Colton Idle
09/20/2024, 9:51 PMColton Idle
09/20/2024, 9:53 PMdb so that it would init the db that was outside of that class.Colton Idle
09/20/2024, 9:53 PMJoffrey
09/20/2024, 9:54 PMColton Idle
09/20/2024, 9:55 PMJoffrey
09/20/2024, 9:55 PM