Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
fix(data): implement data threads
Browse files Browse the repository at this point in the history
Signed-off-by: Rafa Hernandez <[email protected]>
  • Loading branch information
rafaelje authored and Hector Rondon committed Jul 6, 2018
1 parent 4bfa08b commit 29380e4
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions app/src/main/java/org/flyve/mdm/agent/data/database/MqttData.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
*/

import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;

import org.flyve.mdm.agent.data.database.setup.AppDataBase;
import org.flyve.mdm.agent.data.database.entity.MQTT;
import org.flyve.mdm.agent.data.database.setup.AppDataBase;

import java.util.List;

Expand All @@ -57,6 +60,15 @@ public class MqttData {

private AppDataBase dataBase;

private static Handler uiHandler;

static {
uiHandler = new Handler(Looper.getMainLooper());
}
private static void runOnUI(Runnable runnable) {
uiHandler.post(runnable);
}

public MqttData(Context context) {
dataBase = AppDataBase.getAppDatabase(context);
}
Expand Down Expand Up @@ -218,16 +230,22 @@ private String getStringValue(String name) {
}
}

private void setStringValue(String name, String value) {
if(dataBase.MQTTDao().getByName(name).isEmpty()) {
MQTT mqtt = new MQTT();
mqtt.name = name;
mqtt.value = value;
dataBase.MQTTDao().insert(mqtt);
} else {
MQTT mqtt = dataBase.MQTTDao().getByName(name).get(0);
mqtt.value = value;
dataBase.MQTTDao().update(mqtt);
}
private void setStringValue(final String name, final String value) {
Thread t = new HandlerThread("UIHandler") {
@Override
public void run() {
if (dataBase.MQTTDao().getByName(name).isEmpty()) {
MQTT mqtt = new MQTT();
mqtt.name = name;
mqtt.value = value;
dataBase.MQTTDao().insert(mqtt);
} else {
MQTT mqtt = dataBase.MQTTDao().getByName(name).get(0);
mqtt.value = value;
dataBase.MQTTDao().update(mqtt);
}
}
};
t.start();
}
}

0 comments on commit 29380e4

Please sign in to comment.