Приложение Jetpack Compose зависает при запуске ⇐ Android
-
Anonymous
Приложение Jetpack Compose зависает при запуске
I have an android application built by jetpack-compose. When my MainActivity starts, it calls some composables. But the problem is that after showing the first composable, it freezes and I cannot interact with it for a few seconds (about 30 seconds). What could be the problem?
Here is my HomeViewModel (It is the main page corresponding view model.):
... init { val labels = context.resources.getStringArray(R.array.main_page_home_sortings) for (index in 0 until labels.size * 2) { _assetModels.add(MutableStateFlow(PagingData.empty())) } viewModelScope.launch(handler) { getAssetListUseCase.saveAssetListInDatabase() retry() } } fun retry() { var params = arrayListOf( Pair(SortedBy.PRICE, SortingType.ASC), Pair(SortedBy.PRICE, SortingType.DSC), Pair(SortedBy.VOLUME, SortingType.ASC), Pair(SortedBy.VOLUME, SortingType.DSC), Pair(SortedBy.GAINERS, SortingType.ASC), Pair(SortedBy.GAINERS, SortingType.DSC), Pair(SortedBy.MARKET_CAP, SortingType.ASC), Pair(SortedBy.MARKET_CAP, SortingType.DSC), ) for (param in params) { val index = params.indexOf(param) getAssetModels(param.first, param.second, _assetModels[index]) } } fun getAssetModels( sortedBy: SortedBy, sortingType: com.example.ramzinium.data.SortingType, state: MutableStateFlow, ) = CoroutineScope(Dispatchers.IO).launch { jwt?.let { UseCaseInput(it, sortedBy, sortingType) }?.let { getAssetListUseCase.execute(it).distinctUntilChanged() .cachedIn(viewModelScope).collect() { state.value = it _dataLoadedFromNetwork.update { true } } } } Main Activity:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) mainActivityViewModel = ViewModelProvider( this, MainActivityViewModelFactory(navigationImpl) )[MainActivityViewModel::class.java] authenticationViewModel = ViewModelProvider( this, AuthenticationViewModelFactory( navigationImpl, GetUserUseCase( AuthenticationRepositoryImpl( ApiRequest.getInstance() ), context = baseContext ), AuthorizationForgotPasswordUseCase(AuthenticationRepositoryImpl(ApiRequest.getInstance())), baseContext ) { homeViewModel.onUserAuthenticated(it) } )[AuthenticationViewModel::class.java] setContent { val navController = rememberNavController() val coroutineScope = rememberCoroutineScope() NavigationEffects( navigationChannel = mainActivityViewModel.navigationChannel, navHostController = navController ) val paddingValues = WindowInsets.systemBars.asPaddingValues() ExtendedTheme { val originDirection = LocalLayoutDirection.current CompositionLocalProvider(LocalLayoutDirection provides originDirection) { Scaffold(Modifier.padding(paddingValues)) { CompositionLocalProvider(LocalRippleTheme provides NoRippleTheme) { NavHost( navController = navController, startDestination = Destination.MainScreen.fullRoute, modifier = Modifier.padding(it) ) { ... }}...} HomeMainComposable:
fun HomeMainComposable( homeViewModel: HomeViewModel, newsViewModel: NewsViewModel, onNavigate: OnNavigate? = null ) { val pagerPages by remember { mutableStateOf(listOf("home", "market", "news", "converter")) } val icons = listOf( painterResource(id = R.drawable.ic_main_page_home), painterResource(id = R.drawable.ic_main_page_market), painterResource(id = R.drawable.ic_main_page_news), painterResource(id = R.drawable.ic_main_page_converter) ) val navigationDrawerState = homeViewModel.navigationDrawerState.collectAsState() val updateTime = homeViewModel.updateTime.collectAsState() var selectedItem by remember { mutableIntStateOf(0) } var showLoginBottomSheetState = homeViewModel.showLoginBottomSheet.collectAsState() var showNewsFilterBottomSheetState = newsViewModel.showNewsFilter.collectAsState() var showChangeIntervalBottomSheetState by remember { mutableStateOf(false) } val coroutineScope = rememberCoroutineScope() val context = LocalContext.current LaunchedEffect(Unit) { homeViewModel.showLoginBottomSheet(context) } ModalNavigationDrawer(drawerState = navigationDrawerState.value.drawerState, drawerContent = { ... }){ //The resut is just compose code... } } Edit 2 I just realized that when I comment out the method retry, the app won't freeze, even though it is running on IO thread.
Источник: https://stackoverflow.com/questions/780 ... n-start-up
I have an android application built by jetpack-compose. When my MainActivity starts, it calls some composables. But the problem is that after showing the first composable, it freezes and I cannot interact with it for a few seconds (about 30 seconds). What could be the problem?
Here is my HomeViewModel (It is the main page corresponding view model.):
... init { val labels = context.resources.getStringArray(R.array.main_page_home_sortings) for (index in 0 until labels.size * 2) { _assetModels.add(MutableStateFlow(PagingData.empty())) } viewModelScope.launch(handler) { getAssetListUseCase.saveAssetListInDatabase() retry() } } fun retry() { var params = arrayListOf( Pair(SortedBy.PRICE, SortingType.ASC), Pair(SortedBy.PRICE, SortingType.DSC), Pair(SortedBy.VOLUME, SortingType.ASC), Pair(SortedBy.VOLUME, SortingType.DSC), Pair(SortedBy.GAINERS, SortingType.ASC), Pair(SortedBy.GAINERS, SortingType.DSC), Pair(SortedBy.MARKET_CAP, SortingType.ASC), Pair(SortedBy.MARKET_CAP, SortingType.DSC), ) for (param in params) { val index = params.indexOf(param) getAssetModels(param.first, param.second, _assetModels[index]) } } fun getAssetModels( sortedBy: SortedBy, sortingType: com.example.ramzinium.data.SortingType, state: MutableStateFlow, ) = CoroutineScope(Dispatchers.IO).launch { jwt?.let { UseCaseInput(it, sortedBy, sortingType) }?.let { getAssetListUseCase.execute(it).distinctUntilChanged() .cachedIn(viewModelScope).collect() { state.value = it _dataLoadedFromNetwork.update { true } } } } Main Activity:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) mainActivityViewModel = ViewModelProvider( this, MainActivityViewModelFactory(navigationImpl) )[MainActivityViewModel::class.java] authenticationViewModel = ViewModelProvider( this, AuthenticationViewModelFactory( navigationImpl, GetUserUseCase( AuthenticationRepositoryImpl( ApiRequest.getInstance() ), context = baseContext ), AuthorizationForgotPasswordUseCase(AuthenticationRepositoryImpl(ApiRequest.getInstance())), baseContext ) { homeViewModel.onUserAuthenticated(it) } )[AuthenticationViewModel::class.java] setContent { val navController = rememberNavController() val coroutineScope = rememberCoroutineScope() NavigationEffects( navigationChannel = mainActivityViewModel.navigationChannel, navHostController = navController ) val paddingValues = WindowInsets.systemBars.asPaddingValues() ExtendedTheme { val originDirection = LocalLayoutDirection.current CompositionLocalProvider(LocalLayoutDirection provides originDirection) { Scaffold(Modifier.padding(paddingValues)) { CompositionLocalProvider(LocalRippleTheme provides NoRippleTheme) { NavHost( navController = navController, startDestination = Destination.MainScreen.fullRoute, modifier = Modifier.padding(it) ) { ... }}...} HomeMainComposable:
fun HomeMainComposable( homeViewModel: HomeViewModel, newsViewModel: NewsViewModel, onNavigate: OnNavigate? = null ) { val pagerPages by remember { mutableStateOf(listOf("home", "market", "news", "converter")) } val icons = listOf( painterResource(id = R.drawable.ic_main_page_home), painterResource(id = R.drawable.ic_main_page_market), painterResource(id = R.drawable.ic_main_page_news), painterResource(id = R.drawable.ic_main_page_converter) ) val navigationDrawerState = homeViewModel.navigationDrawerState.collectAsState() val updateTime = homeViewModel.updateTime.collectAsState() var selectedItem by remember { mutableIntStateOf(0) } var showLoginBottomSheetState = homeViewModel.showLoginBottomSheet.collectAsState() var showNewsFilterBottomSheetState = newsViewModel.showNewsFilter.collectAsState() var showChangeIntervalBottomSheetState by remember { mutableStateOf(false) } val coroutineScope = rememberCoroutineScope() val context = LocalContext.current LaunchedEffect(Unit) { homeViewModel.showLoginBottomSheet(context) } ModalNavigationDrawer(drawerState = navigationDrawerState.value.drawerState, drawerContent = { ... }){ //The resut is just compose code... } } Edit 2 I just realized that when I comment out the method retry, the app won't freeze, even though it is running on IO thread.
Источник: https://stackoverflow.com/questions/780 ... n-start-up
Мобильная версия