Closed dshukertjr closed 3 months ago
One problem I can observe (and I'm wondering why this isn't mentioned in the reference docs anymore) is that the collect
method call suspends the entire coroutine until it's closed. That means the subscribe
method never gets called. You could try to collect the flow in another coroutine:
changeFlow.onEach {
when (it) {
is PostgresAction.Delete -> println("Deleted: ${it.oldRecord}")
is PostgresAction.Insert -> println("Inserted: ${it.record}")
is PostgresAction.Select -> println("Selected: ${it.record}")
is PostgresAction.Update -> println("Updated: ${it.oldRecord} with ${it.record}")
}
}.launchIn(composableScope) //same as launching a new coroutine and collecting the flow
channel.subscribe()
Thanks, will try it out tomorrow to see if your code works. Let's definitely add this one in the reference docs.
Does the following code then ever work? Should there always be a launchIn()
call on the Flow?
val channel = supabase.channel("channelId") {
//optional config
}
val changeFlow = channel.postgresChangeFlow<PostgresAction>(schema = "public")
//Collect the flow
changeFlow.collect {
when(it) {
is PostgresAction.Delete -> println("Deleted: ${it.oldRecord}")
is PostgresAction.Insert -> println("Inserted: ${it.record}")
is PostgresAction.Select -> println("Selected: ${it.record}")
is PostgresAction.Update -> println("Updated: ${it.oldRecord} with ${it.record}")
}
}
channel.subscribe()
Well, as the collect
method blocks the coroutine, it can't work. So you can either launch a new coroutine and call collect
within, or use my shorthand method from above
Good to know. Let's keep in mind that it's generally better if the code samples on the reference docs just work by copying and pasting. Let me fix the code on the reference docs 👍
Alright, thanks!
@jan-tennert
Related to this, when working with Supabase realtime, would you ever use .collect
on a Flow? It seems like in order to use the .launchIn()
, you need to use onEach
, so wondering if we should replace all the code samples on using onEach
instead of collect
.
I'm probably missing something, but I wanted to understand the intent of using collect
in the sample code.
Well, launchIn
is basically calling collect
under the hood, just in a new coroutine. You could also use this code:
flow
.onEach { }
.collect()
And it would do the same thing, apart from suspending the current coroutine. For the snippet it's probably best to use the launchIn
shortcut, so we can call methods after.
The docs also point to the Kotlin Flow docs, but I agree we should have copy-pastable snippets.
Thanks for the clarification! I can work on the docs then!
And will tag you for review!
General Info
Version(s)
2.4.1
Kotlin Target(s) and their respective versions
Android targetSdk 34
What happened? (include your code)
I am unable to get any realtime data using the realtime feature of this library.
Steps To Reproduce (optional)
class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { ExampleTheme { Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { SamplePage() } } } } }
@Composable fun SamplePage() { val composableScope = rememberCoroutineScope()
}
Relevant log output (optional)
No response