Detcach samples from the session after querying, to save memory

master
cpfeiffer 2016-08-27 21:40:46 +02:00
parent fb30321cca
commit bfaaed7e5c
4 changed files with 17 additions and 0 deletions

View File

@ -93,9 +93,20 @@ public abstract class AbstractSampleProvider<T extends AbstractActivitySample> i
for (T sample : samples) {
sample.setProvider(this);
}
detachFromSession();
return samples;
}
/**
* Detaches all samples of this type from the session. Changes to them may not be
* written back to the database.
*
* Subclasses should call this method after performing custom queries.
*/
protected void detachFromSession() {
getSampleDao().detachAll();
}
private WhereCondition[] getClauseForActivityType(QueryBuilder qb, int activityTypes) {
if (activityTypes == ActivityKind.TYPE_ALL) {
return NO_CONDITIONS;

View File

@ -9,6 +9,10 @@ import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
/**
* Interface to retrieve samples from the database, and also create and add samples to the database.
* There are multiple device specific implementations, this interface defines the generic access.
*
* Note that the provided samples must typically be considered read-only, because they are immediately
* removed from the session before they are returned.
*
* @param <T> the device/provider specific sample type (must extend AbstractActivitySample)
*/
public interface SampleProvider<T extends AbstractActivitySample> {

View File

@ -57,6 +57,7 @@ public class PebbleHealthSampleProvider extends AbstractSampleProvider<PebbleHea
}
}
}
detachFromSession();
return samples;
}

View File

@ -50,6 +50,7 @@ public class EntitiesTest {
@Before
public void setUp() throws GBException {
// doesn't work with Robolectric yet
// dbHandler = GBApplication.acquireDB();
// daoSession = dbHandler.getDaoSession();
DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(app, null, null);