resume method

Future<int> resume()

Restore and resume all study deployments which were running on this client manager when the app was killed / stopped (e.g., by the OS).

This method is useful on app restart, since it will restore and resume all sampling on this client. Data sampling will be resumed for studies which were running (i.e., having StudyStatus.Running) when the app was closed.

Returns the number of restored studies, if any (may be zero). To access a list of resumed studies, use studies after this resume method has ended.

Implementation

Future<int> resume() async {
  info('$runtimeType - restoring all study deployments');
  var persistentStudies = await Persistence().getAllStudyDeployments();

  debug('$runtimeType - studies: $persistentStudies');

  for (var study in persistentStudies) {
    debug('$runtimeType - study status: ${study.status}');

    // only add deployed, running or stopped studies
    if (study.status == StudyStatus.Deployed ||
        study.status == StudyStatus.Running ||
        study.status == StudyStatus.Stopped) {
      var newStudy =
          await addStudy(study.studyDeploymentId, study.deviceRoleName);
      newStudy.status = study.status;
      final controller = getStudyRuntime(study);

      // deploy the study using the cached deployment
      await controller?.tryDeployment(useCached: true);
      await controller?.configure();

      // if this study was running when the app was closed, restart sampling
      if (study.status == StudyStatus.Running) controller?.start();
    }
  }
  _state = ClientManagerState.started;
  return studyCount;
}