/*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2020 Damyan Ivanov.
* This file is part of MoLe.
* MoLe is free software: you can distribute it and/or modify it
* under the term of the GNU General Public License as published by
class DbOpItem {
String sql;
Object[] params;
- public DbOpItem(String sql, Object[] params) {
+ Runnable onReady;
+ public DbOpItem(String sql, Object[] params, Runnable onReady) {
this.sql = sql;
this.params = params;
+ this.onReady = onReady;
+ }
+ public DbOpItem(String sql, Object[] params) {
+ this(sql, params, null);
}
public DbOpItem(String sql) {
- this(sql, null);
+ this(sql, null, null);
}
}
/*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2020 Damyan Ivanov.
* This file is part of MoLe.
* MoLe is free software: you can distribute it and/or modify it
* under the term of the GNU General Public License as published by
static private final BlockingQueue<DbOpItem> queue = new LinkedBlockingQueue<>();
static private DbOpRunner runner;
synchronized static public void init() {
- if (runner != null) return;
+ if (runner != null)
+ return;
debug("opQueue", "Starting runner thread");
runner = new DbOpRunner(queue);
runner.start();
static public void done() {
runner.interrupt();
}
- public static void add(String sql, Object[] params) {
+ public static void add(String sql, Object[] params) {add(sql, params, null);}
+ public static void add(String sql, Object[] params, Runnable onReady) {
init();
debug("opQueue", "Adding " + sql);
- queue.add(new DbOpItem(sql, params));
+ queue.add(new DbOpItem(sql, params, onReady));
}
static void add(String sql) {
queue.add(new DbOpItem(sql));
/*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2020 Damyan Ivanov.
* This file is part of MoLe.
* MoLe is free software: you can distribute it and/or modify it
* under the term of the GNU General Public License as published by
while (!interrupted()) {
try {
DbOpItem item = queue.take();
- debug("opQrunner", "Got "+item.sql);
- SQLiteDatabase db = App.getDatabase();
- debug("opQrunner", "Executing "+item.sql);
- db.execSQL(item.sql, item.params);
+ debug("opQrunner", "Got " + item.sql);
+ {
+ SQLiteDatabase db = App.getDatabase();
+ debug("opQrunner", "Executing " + item.sql);
+ db.execSQL(item.sql, item.params);
+ }
+ if (item.onReady != null)
+ item.onReady.run();
}
catch (InterruptedException e) {
break;