How do I debug this? - It crashes only on Android,...
# multiplatform
s
How do I debug this? - It crashes only on Android, not desktop, iOS or web:
Copy code
FATAL EXCEPTION: main
                                                                                                    Process: dev.upvote.debug, PID: 22296
There's no other traceback; I've tried running with debugger also.
p
Real device or emulator?
g
You should provide more information about when it is crashing. For example, does it crash on app start up or after you perform a specific action? Also, I suspect there would be more information or logs about what's going on in Logcat
p
Usually incomplete logs is a sign or process being abruptly killed. Could be exhausted use of memory, some permission issues. Things of that nature
Emulators some times run out of memory that's why I asked
s
Emulator yes, but I have plenty of memory remaining. Open-sourcing everything but it's nowhere near ready for outside contribution: https://github.com/upvote-dev/UpvoteApp/blob/465e37d/composeApp/src/commonMain/kotlin/dev/upvote/presentation/profile/ProfileScreen.kt#L59 To replicate open the app on Android and click "Profile" in bottom left. (notably I have figured out neither Decompose nor a nice way of sharing data between `Composable`s yet so have a lot of ugly/verbose hacks… so feel free to ignore that part and just see if you can replicate the crash)
Any clue how to debug this?
p
Not really, I don't see anything dramatic in code. It only crashes when you visit this screen? Can you debug until the line of code that causes the crash?
g
There's tons of stuff you can try: • Copy pasting this code into ChatGPT and telling it very specifically what's happening, how it's crashing for one platform but not another • Systematically comment out any potentially problematic pieces of code until the crash stops, use that to determine which specific bit of code is causing the crash
s
Yeah I traced it all the way through and it seems to be the
viewModel.getProfile()
line. When that's removed it doesn't crash. But there's nothing fancy going on there; so a bit confused.
g
Have you tried asking ChatGPT why that line might cause a crash on Android specifically? Next you could comment out specific parts of
getProfile()
to narrow down exactly what's going wrong there. In general having lifecycle listeners in Composables strikes me as a weird and potentially dangerous pattern
Copy code
OnLifecycleEvent { _, event ->
        when (event) {
            Lifecycle.Event.ON_RESUME -> {
                viewModel.getProfile()
            }
            else -> {}
        }
    }
p
What is inside that function? Do the same procedure there and find the last piece of the puzzle
g
Also noting that this code is also super dangerous
Copy code
val profile = profileContentRepository.getProfile().first()
                profile.also {
                    setProfile(profile = profile!!)
                }
• .first() will cause a crash if the flow does not have a value, prefer to use
.firstOrNull()
profile!!
will cause a crash if
profile
is null, prefer to do something like
Copy code
profile?.let {
   setProfile(profile = it)
}
☝️ 1
s
In general having lifecycle listeners in Composables strikes me as a weird and potentially dangerous pattern
Agreed, only added them as a debug measure Oh I thought
profile.also
did that. Ok changed to your syntax. As for
first
, it's
Copy code
override fun getProfile() = flow {
        emit(profileApi.getProfile())
    }
Where
profileApi.getProfile
is either going to error [404] or have one value [
.first
guaranteed to succeed]