How to merging between 2 realm files in Flutter
Hi @zmonx_gg!
There is no automated way for merging for now. But you can open both realm files and copy the data between them. Be sure to re-create the primary keys in case there are duplicated. It will be good to make a copy of the realm files before this operation.
Here is an example:
final configRead = Configuration.local([Product.schema], isReadOnly: true, path: r"absolute path to realm file to read");
final realmRead = Realm(configRead);
final configWrite = Configuration.local([Product.schema], path: r"absolute path to realm file to write");
final realmWrite = Realm(configWrite);
realmWrite.write(() {
realmWrite.addAll<Product>(realmRead.all<Product>().map((p) => Product(ObjectId(), p.name)));
});
realmWrite.close();
realmRead.close();
1 Like