sync method

Future<SyncResult> sync(
  1. String userId, {
  2. bool force = false,
  3. SyncOptions? options,
})

Executes a full synchronization cycle for the specified user.

Pushes pending local changes to remote, then pulls remote changes. Use force to bypass typical sync conditions (connectivity, timing). Updates internal metrics and statistics.

Returns SyncResult containing detailed outcome information.

Implementation

Future<SyncResult> sync(
  String userId, {
  bool force = false,
  SyncOptions? options,
}) async {
  _ensureInitializedAndNotDisposed();

  if (userId.isEmpty) {
    throw ArgumentError.value(userId, 'userId', 'Must not be empty');
  }

  try {
    await _ensureUserInitialized(userId);

    final engine = _syncEngine!;
    final result = await engine.synchronize(
      userId,
      force: force,
      options: options,
    );

    _updateMetricsAndStatistics(result, userId);

    return result;
  } on Object catch (e, stack) {
    _logger.error('Sync failed for user: $userId', stack);
    _emitError(userId, 'Sync failed: $e', stack);
    rethrow;
  }
}