Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
Device Sync - Flutter SDK

Sync Data from Multiple Processes

You can use a single realm to synchronize data from multiple processes using Atlas Device Sync.

Examples of scenarios where you might want to open a synced realm in multiple processes include:

  • A multi-window desktop application where each window writes to the same synced realm.

  • A server application that writes to a synced realm from multiple processes.

To open a single synced realm from multiple processes, perform the following:

  1. Create a single main process that opens a Realm using a standard flexible sync configuration. The main process handles synchronization.

  2. Crear uno o más procesos secundarios que abran el mismo realm utilizando una configuración de sincronización desconectada. Con una configuración de sincronización desconectada, los procesos secundarios leen y escriben datos en el Realm sin gestionar la sincronización. El proceso principal gestiona la sincronización de todos los datos para el proceso secundario.

Para abrir un reino sincronizado en el proceso principal, utilice el Configuración.flexibleSync()constructor. Para obtener más información, consulte Abrir un dominio sincronizado.

main_process.dart
// Same realm file location as secondary process
final realmPath =
path.join(Configuration.defaultStoragePath, 'synced.realm');
final flexibleConfig =
Configuration.flexibleSync(currentUser, schema, path: realmPath);
final realmWithSync = Realm(flexibleConfig);

Para abrir un reino sincronizado en un proceso secundario, cree un Configuration Con el constructor Configuration.disconnectedSync(). Incluya el esquema y cualquier argumento opcional adicional.

secondary_process.dart
// Same realm file location as primary process
final sameRealmPath =
path.join(Configuration.defaultStoragePath, 'synced.realm');
final disconnectedSyncConfig =
Configuration.disconnectedSync(schema, path: sameRealmPath);
final realmWithDisconnectedSync = Realm(disconnectedSyncConfig);

When sharing the same realm file between multiple processes, Realm has auto-refresh built-in. Data written from one process is visible to the other processes. Generally, you do not need to write additional logic to refresh data across processes.

However, occasionally the refresh may not happen immediately. In this case, you can trigger a manual refresh in a process with Realm.refresh() or Realm.refreshAsync().

To synchronously force update notifications for changes made by another process, call Realm.refresh().

main_process.dart
// Add object in one process
realm.write(() {
realm.add(Person('John'));
});
secondary_process.dart
// Call realm.refresh() in the secondary process
// to trigger the data written in the main process
// to register in the secondary process.
realm.refresh();
final john = realm.find<Person>('John');

Alternatively, you can use Realm.refreshAsync() to asynchronously force update notifications for changes made by another process.

secondary_process.dart
// Asynchronously refresh the realm in the background.
await realm.refreshAsync();
final john = realm.find<Person>('John');

Volver

Manage Sync Session