简体   繁体   English

如何确保我的后台线程绑定到我的 ViewModel 而不是 Zombie Activity? (安卓/Java/MVVM)

[英]How can I ensure my background thread is tied to my ViewModel and not a Zombie Activity? (Android/Java/MVVM)

I recently implemented a ViewModel and some LiveData components to hold a list of results fetched from a webpage on my Android App in Java.我最近在 Java 中实现了一个 ViewModel 和一些 LiveData 组件来保存从我的 Android 应用程序上的网页获取的结果列表。

My goal is to ensure that on configuration changes (changing from portrait to landscape for example) that any background threads fetching data are not tied to dead/Zombie activities and tied to the ViewModel itself so that there aren't duplicated threads causing memory leaks.我的目标是确保在配置更改时(例如从纵向更改为横向),任何获取数据的后台线程都不会与死/僵尸活动相关联,并且不会与 ViewModel 本身相关联,这样就不会有导致 memory 泄漏的重复线程。

My question is: is starting a thread from within a ViewModel enough to ensure the thread is tied to the ViewModel and not possibly a zombie Activity?我的问题是:从 ViewModel 中启动一个线程是否足以确保该线程与 ViewModel 绑定,而不是僵尸活动? I understand the ViewModel is lifecycle aware, so it does not "die" or get destroyed upon configuration changes.我了解 ViewModel 具有生命周期意识,因此它不会“死亡”或在配置更改时被破坏。

I have my Application class hold a global ExecutorService and Handler:我的应用程序 class 拥有一个全局 ExecutorService 和 Handler:

public class MyApplication extends Application {

    private ExecutorService executorService = Executors.newSingleThreadExecutor();
    private Handler mainThreadHandler = Handler.createAsync(Looper.getMainLooper());

    public Handler getMainThreadHandler() {
        return mainThreadHandler;
    }

    public ExecutorService getExecutorService() {
        return executorService;
    }

When the user clicks a button to start the fetching of data on the Activity's View, the Activity calls the search function of the ViewModel and passes in the handler and executor service:当用户点击按钮开始在Activity的View上获取数据时,Activity调用ViewModel的search function并传入handler和executor服务:

public class MyViewModel extends ViewModel {

    ...

    public void SearchForResults(final String state, final String name, final ExecutorService  executorService, final Handler handler)
    {

        executorService.submit(new Runnable() {
            @Override
            public void run() {
                /*NETWORK LOGIC - FILTERING,ETC*/

                ...

                /* to update LiveData class variables of ViewModel */
                handler.post(new Runnable() {  
                    @Override
                    public void run() {
                        liveData1.setValue(true);
                        liveData2.setValue(false);
                    }
                });

            }
        });
    }

Will these threads be held to the ViewModel in case the Activity is destroyed while they are still running in the background?如果 Activity 在后台运行时被销毁,这些线程是否会保留在 ViewModel 中? How can we determine what the "owner" of the threads are?我们如何确定线程的“所有者”是什么?

Is it proper to use a UI thread handler to update class variables of the ViewModel from the background thread?使用 UI 线程处理程序从后台线程更新 ViewModel 的 class 变量是否合适?

Thanks for your help.谢谢你的帮助。

My question is: is starting a thread from within a ViewModel enough to ensure the thread is tied to the ViewModel and not possibly a zombie Activity?我的问题是:从 ViewModel 中启动一个线程是否足以确保该线程与 ViewModel 绑定,而不是僵尸活动?

Yes, though you still have to be careful of any references your thread is holding on to.是的,尽管您仍然必须小心您的线程所持有的任何引用 If it is holding on to an Activity or View or something else that itself is holding on to the Activity that you might have passed to the ViewModel, then you have a problem.如果它持有 Activity 或 View 或其他本身持有您可能已传递给 ViewModel 的 Activity 的东西,那么您就有问题了。

Will these threads be held to the ViewModel in case the Activity is destroyed while they are still running in the background?如果 Activity 在后台运行时被销毁,这些线程是否会保留在 ViewModel 中?

Yes.是的。

How can we determine what the "owner" of the threads are?我们如何确定线程的“所有者”是什么?

Well, the "owner" is the thing that started the thread so you should know who the owner is.好吧,“所有者”是启动线程的东西,所以您应该知道所有者是谁。 You shouldn't have to gleam that from the thread itself.您不必从线程本身中看到它。

Is it proper to use a UI thread handler to update class variables of the ViewModel from the background thread?使用 UI 线程处理程序从后台线程更新 ViewModel 的 class 变量是否合适?

I don't know about "proper" - Kotlin and coroutines are the rage these days - but since you're using Java, it's probably fine.我不知道“正确”——Kotlin 和协程现在风靡一时——但既然你使用的是 Java,它可能没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的活动中获取 viewpager 数据 android java - How can I get viewpager data in my activity android java Android Studio Java:如果产品已经存在于我的购物车活动中,我该如何更新我的数量? - Android Studio Java: How can I update my quantity if the product already exist's in my Cart Activity? ¿ 如何在后台启动我的应用程序或活动? - ¿How can i launch my app or an activity in background? 如何安排后台线程在应用程序中播放声音? - How can I schedule a background thread to play sounds in my app? 如何在我的活动中查看寻呼机数据 android java - How can I get view pager data in my activity android java 如何从Activity线程修改Adapter中的行? - How can I modify the rows in my Adapter from my Activity thread? 如何在Java中创建可以确保延迟加载我的JPA数据库实体的单元测试? - How Can I create a Unit Test in Java that Can ensure that my JPA database entities are being loaded lazily? 如何在Android应用程序中更改(线程的)堆栈大小? - How can I change the stack size (of a thread) in my Android Application? 我如何使我的android程序的背景变为Java? - How do i make the background of my android program colored in java? 如何确定我的android runnable在辅助线程上启动? - How can I be sure that my android runnable started on a worker thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM