diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java index ab2e6b4e87..9e09b661c1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java @@ -381,16 +381,14 @@ public class TreatmentService extends OrmLiteBaseService { QueryBuilder queryBuilder = getDao().queryBuilder(); Where where = queryBuilder.where(); where.eq("pumpId", pumpId); - PreparedQuery preparedQuery = queryBuilder.prepare(); - List result = getDao().query(preparedQuery); - switch (result.size()) { - case 0: - return null; - case 1: - return result.get(0); - default: - throw new RuntimeException("Multiple records with the same pump id found: " + result.toString()); - } + queryBuilder.orderBy("date", true); + + List result = getDao().query(queryBuilder.prepare()); + if (result.isEmpty()) + return null; + if (result.size() > 1) + log.warn("Multiple records with the same pump id found (returning first one): " + result.toString()); + return result.get(0); } catch (SQLException e) { throw new RuntimeException(e); }