在其他请求中提出请求后,应用程序放慢速度

发布于 2025-02-01 17:19:20 字数 1981 浏览 2 评论 0原文

我正在根据用户名提出coroutines的请求,该列表返回object< profile>的列表,而在该列表中,我正在用每个对象提出另一个请求,然后切换并传递信息在另一个屏幕上,但是这样的过程使应用程序超级慢,我想找到一种更好的方法或一种不使此过程如此慢的方法。在这里,我的代码

片段从我开始的位置以及应用程序变得超级慢的

 emptyHomeViewModel.getPlayersListByName(text)
                emptyHomeViewModel.listOfPlayersByNameLiveData.observe(viewLifecycleOwner) { playersByName ->
                    emptyHomeViewModel.getPlayersProfileByName(playersByName)
                    emptyHomeViewModel.listOfProfilesByID.observe(viewLifecycleOwner) { profiles ->
                        if (profiles != null) {
                            val list: Array<Profile> = profiles.toTypedArray()
                            bundle = Bundle().apply {
                                putSerializable("user", list)
                            }
                            findNavController().navigate(
                                R.id.action_emptyHomeFragment_to_selectUserFragment,
                                bundle
                            )
                        }
                    }
                }

视图模型从我执行coroutines并向API提出请求的位置

 fun getPlayersListByName(playerName: String) = viewModelScope.launch {
        val playersList = getPlayersByPersonaNameUseCase.getPlayersByName(playerName)
        if (playersList != null) {
            _listOfPlayersByNameLiveData.postValue(playersList)
        }
    }


 fun getPlayersProfileByName(playersByName: List<PlayerByPersonaNameItem>?) =
        viewModelScope.launch {
            var playersProfileList: ArrayList<Profile> = arrayListOf()
            if (playersByName != null) {
                for (player in playersByName) {
                    getPlayerByIDUseCase.getPlayerById(player.accountId)
                        ?.let { playersProfileList.add(it) }
                }
                _listOfProfilesByID.postValue(playersProfileList)
            }
        }

I am making a request with coroutines based on a user name, which returns a list of Object<Profile>, and with that list I am making another request with each object, and then switching and passing the info to another screen, but such process is making the app super slow and I would like to find a better way or a way to not making this process so slow. Here my code

Fragment from where I am starting the process and where the app is getting super slow

 emptyHomeViewModel.getPlayersListByName(text)
                emptyHomeViewModel.listOfPlayersByNameLiveData.observe(viewLifecycleOwner) { playersByName ->
                    emptyHomeViewModel.getPlayersProfileByName(playersByName)
                    emptyHomeViewModel.listOfProfilesByID.observe(viewLifecycleOwner) { profiles ->
                        if (profiles != null) {
                            val list: Array<Profile> = profiles.toTypedArray()
                            bundle = Bundle().apply {
                                putSerializable("user", list)
                            }
                            findNavController().navigate(
                                R.id.action_emptyHomeFragment_to_selectUserFragment,
                                bundle
                            )
                        }
                    }
                }

ViewModel from where I am executing the coroutines and making the request to the API

 fun getPlayersListByName(playerName: String) = viewModelScope.launch {
        val playersList = getPlayersByPersonaNameUseCase.getPlayersByName(playerName)
        if (playersList != null) {
            _listOfPlayersByNameLiveData.postValue(playersList)
        }
    }


 fun getPlayersProfileByName(playersByName: List<PlayerByPersonaNameItem>?) =
        viewModelScope.launch {
            var playersProfileList: ArrayList<Profile> = arrayListOf()
            if (playersByName != null) {
                for (player in playersByName) {
                    getPlayerByIDUseCase.getPlayerById(player.accountId)
                        ?.let { playersProfileList.add(it) }
                }
                _listOfProfilesByID.postValue(playersProfileList)
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后eg是否自 2025-02-08 17:19:20

您实际上可以并行加载配置文件,以防止它们接一个地加载,以减少加载数据的时间:

fun getPlayersProfileByName(playersByName: List<PlayerByPersonaNameItem>?) =
    viewModelScope.launch {
        val playersProfileList: List<Profile> = playersByName?.map { player ->
            async {
                getPlayerByIDUseCase.getPlayerById(player.accountId)
            }
        }.awaitAll().filterNotNull()
        _listOfProfilesByID.postValue(playersProfileList)
    }

您也可以通过删除附加livedata观察者和调用getPlayerSplayersProfilebyByname 获得playerSlist后立即:

fun getPlayersListByName(playerName: String) = viewModelScope.launch {
    val playersList = getPlayersByPersonaNameUseCase.getPlayersByName(playerName)
    getPlayersProfileByName(playersList)
}

You can actually load profiles in parallel, preventing loading them one after another, to decrease time of loading data:

fun getPlayersProfileByName(playersByName: List<PlayerByPersonaNameItem>?) =
    viewModelScope.launch {
        val playersProfileList: List<Profile> = playersByName?.map { player ->
            async {
                getPlayerByIDUseCase.getPlayerById(player.accountId)
            }
        }.awaitAll().filterNotNull()
        _listOfProfilesByID.postValue(playersProfileList)
    }

Also you can improve it a little bit by removing additional LiveData observer and calling getPlayersProfileByName right after you get playersList:

fun getPlayersListByName(playerName: String) = viewModelScope.launch {
    val playersList = getPlayersByPersonaNameUseCase.getPlayersByName(playerName)
    getPlayersProfileByName(playersList)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文