我正在尝试使用Room Persistence库获取/存储数据。从表中获取所有数据的调用确实很慢,我使用nanoTime
将其定时为大约1800ms,我认为这太慢了。可能出什么问题了?
更多信息:
nanoTime
)AsyncTask
查询在DbDao中定义为
@Query("SELECT * FROM events")
List<Event> getAllEvents();
答案 0 :(得分:1)
异步任务不利于处理Room数据库。我建议您使用executor并使用它来创建单例类。代码如下。
document.getElementById
现在使这个新类完成后,在appexecutor线程中执行所有数据库访问功能。这比使用异步任务和内置Java线程库要好。还要确保任何数据访问功能都不会在UI线程上运行。 如果您发布了更多代码,我将可以查明确切的问题。 最后一件事在您的代码中按如下方式使用它-
import android.os.Looper;
import android.support.annotation.NonNull;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class AppExecutor {
private static final Object LOCK = new Object();
private static AppExecutor sInstance;
private final Executor diskIO;
private final Executor mainThread;
private final Executor networkIO;
public AppExecutor(Executor diskIO, Executor mainThread, Executor networkIO) {
this.diskIO = diskIO;
this.mainThread = mainThread;
this.networkIO = networkIO;
}
public static AppExecutor getInstance(){
if (sInstance==null){
synchronized (LOCK){
sInstance = new AppExecutor(Executors.newSingleThreadExecutor(),Executors.newFixedThreadPool(3),new MainThreadExecutor());
}
}
return sInstance;
}
public Executor diskIO() {
return diskIO;
}
public Executor mainThread() {
return mainThread;
}
public Executor networkIO() {
return networkIO;
}
private static class MainThreadExecutor implements Executor{
private android.os.Handler mainThreadHandler = new android.os.Handler(Looper.getMainLooper());
@Override
public void execute(@NonNull Runnable runnable) {
}
}
}