gpt4 book ai didi

android - 如何在 ui 线程上注入(inject)并等待注入(inject)完成后再继续?

转载 作者:行者123 更新时间:2023-11-29 01:55:29 24 4
gpt4 key购买 nike

在 Dagger 中,您必须在 UI 线程上注入(inject)。当运行 JUnit 时,您并不是在 UI 线程上运行。为了让它工作,我们必须在 UI 线程上发布一个 Runnable 并等待它完成注入(inject)。

我的代码是这样的:

    public void inject(final Object object) {
// We always has to inject on the main thread.
final CountDownLatch latch = new CountDownLatch(1);
Handler mainHandler = new Handler(FodoApplication.getAppContext().getMainLooper());
Runnable myRunnable = new Runnable() {
public void run() {
objectGraph.inject(object);
latch.countDown();
}
};
mainHandler.post(myRunnable);
try {
latch.await();
} catch (InterruptedException e) {
Log.e(TAG, "", e);
}
}

问题是调用 latch.await() 时 myRunnable 没有启动。在运行 JUnit 时让 Dagger 注入(inject)的最佳方法是什么?

最佳答案

关于android - 如何在 ui 线程上注入(inject)并等待注入(inject)完成后再继续?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15443268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com