private void PersistentRecords(object state)
        {
            Debug.WriteLine($"{nameof(PersistentRecords)} is running on thread id={Thread.CurrentThread.ManagedThreadId}");

            if (_isIdle)
            {
                return;
            }

            if (_mainRepositoryUnavailable)
            {
                var result = TryGetFromMainRepository();

                if (result.Item2 == false)
                {
                    BackupLocally();
                    return;
                }
                else
                {
                    _mainRepositoryUnavailable = false;

                    if (result.Item1 != null)
                    {
                        _bufferedTodayAppRecord.MergeWith(result.Item1);
                        _backupRepository.Delete(_bufferedTodayAppRecord.Id);
                    }
                }
            }

            try
            {
                var todayPersistentRecord = _repository.Get(AppUsageRecord.GetGeneratedId(DateTime.Now));
                if (todayPersistentRecord != null)
                {
                    todayPersistentRecord.ActiveApps = _bufferedTodayAppRecord.ActiveApps;

                    _repository.Update(todayPersistentRecord, todayPersistentRecord.Id);
                }
                else
                {
                    _repository.Add(_bufferedTodayAppRecord);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Error when persisting data: {ex.Message}");

                LoggingService.LogException(ex);

                _mainRepositoryUnavailable = true;

                this.PersistentRecords(state);
            }


            Debug.WriteLine("Persisting data sucessfully");
        }
Esempio n. 2
0
 private void BackupLocally()
 {
     _backupRepository.Update(_bufferedTodayAppRecord, _bufferedTodayAppRecord.Id);
 }