updateSyncDownRetry method

Future<bool> updateSyncDownRetry(
  1. String clientReferenceId
)

Implementation

Future<bool> updateSyncDownRetry(
  String clientReferenceId,
) async {
  final oplogs = isar.opLogs
      .filter()
      .clientReferenceIdEqualTo(clientReferenceId)
      .findAllSync();

  if (oplogs.isEmpty) {
    throw AppException('OpLog not found for id: $clientReferenceId');
  }
  bool markAsNonRecoverable = false;
  for (final oplog in oplogs) {
    final entry = OpLogEntry.fromOpLog<T>(oplog);
    final syncDownRetryCount =
        entry.syncDownRetryCount < 0 ? 0 : entry.syncDownRetryCount;
    OpLogEntry updatedEntry = entry.copyWith(
      syncDownRetryCount: syncDownRetryCount + 1,
    );
    if (updatedEntry.syncDownRetryCount >= 5) {
      markAsNonRecoverable = true;
      updatedEntry = updatedEntry.copyWith(nonRecoverableError: true);
    }

    isar.writeTxnSync(() {
      isar.opLogs.putSync(updatedEntry.oplog);
    });
  }

  // [TODO] need to cross check only first records is failing

  if (oplogs.first.syncDownRetryCount == 1) {
    await Future.delayed(const Duration(seconds: 1));
  } else {
    await Future.delayed(Duration(
      seconds: 5 * oplogs.first.syncDownRetryCount,
    ));
  }

  return markAsNonRecoverable;
}