PersistenceService
Stores running studies and the user task queue in a local SQLite database, so they survive an app restart.
A singleton, accessed as PersistenceService(). Initialized by SmartPhoneClientManager.configure, which then restores studies and tasks. It stores:
- Running studies on the phone as managed by the SmartphoneClientRepository
- User tasks on the task queue as managed by the AppTaskController
After init, changes are saved automatically: it listens to SmartphoneClientRepository.studyStatusEvents and AppTaskController.userTaskEvents. Database errors are logged, not thrown.
This is separate from the measurement database used by SQLiteDataManager.
Studies are stored in the studies table and user tasks are stored in the task_queue table.
The path and filename format for the database is
~/carp.db
where ~ is the folder where SQLite places its database files.
On iOS, this is the NSDocumentsDirectory and the files can be accessed via the MacOS Finder.
On Android, Flutter files are stored in the databases directory, which is located in the data/data/<package_name>/databases/ folder. Files can be accessed via AndroidStudio.
Constructors
PersistenceService
Get the singleton persistence layer.
Constants
CREATED_ON_COLUMN
DATABASE_NAME
DATABASE_VERSION
Schema version. Version 1 databases (including CAMS 1.x) are migrated on init.
DEPLOYED_ON_COLUMN
DEPLOYMENT_COLUMN
DEPLOYMENT_STATUS_COLUMN
DEVICE_ROLE_NAME_COLUMN
ID_COLUMN
PARTICIPANT_ID_COLUMN
PARTICIPANT_ROLE_NAME_COLUMN
SAMPLING_STATUS_COLUMN
STUDY_DEPLOYMENT_ID_COLUMN
STUDY_ID_COLUMN
STUDY_TABLE_NAME
TASK_COLUMN
TASK_ID_COLUMN
TASK_QUEUE_TABLE_NAME
UPDATED_ON_COLUMN
Properties
databaseName
Full path and name of the database.
databasePath
The folder holding the database. Set by init.
Methods
close
Closes the database. After this, no study or task can be read or saved.
getAllStudies
Get the list of all studies previously stored on this phone.
Returns an empty list if no studies are stored or loading fails.
getStudy
Return the SmartphoneStudy with studyDeploymentId and deviceRoleName, or null when no such study is found.
getUserTasks
- SmartphoneStudy? study
Get the list of UserTaskSnapshot for study. If study is null, all user tasks are returned.
init
Opens (and if needed creates or migrates) the database, and starts saving study and task changes. Must be called before any other method.
removeStudy
- Study<
PrimaryDeviceDeployment> study
Removes study from the database. Its user tasks are not removed; use removeUserTasks for that.
removeUserTasks
- Study<
PrimaryDeviceDeployment> ? study
Remove the list of user tasks for study. If study is null, all user tasks are removed.
saveStudy
- SmartphoneStudy study
Saves study, replacing any existing row for it. Returns true if successful.
saveUserTask
Saves task to the task queue table, or deletes it if its state is UserTaskState.dequeued.
updateStudy
- SmartphoneStudy study
Updates the stored study, matched on study deployment ID and device role name. Returns true if successful.