Atlas Device SDK 已弃用。 有关详细信息,请参阅弃用页面。
设置 Device Sync
1
在 中配置Atlas Device SyncAtlas App Services
在将 Device Sync 与 Realm Flutter SDK 结合使用之前,您必须创建一个启用了 Device Sync 和身份验证的 Atlas App Services App。
要学习;了解如何在应用中设立Device Sync ,请参阅App Services文档中的 Atlas Device Sync入门。
2
连接到Atlas App Services后端
Initialize the an App instance to connect to your App Services App. Pass the App ID for your App, which you can find in the App Services UI.
final app = App(AppConfiguration(appId));
4
打开同步 Realm
使用“Flexible Sync”配置将 Realm 作为同步 Realm 打开。 还要添加订阅以同步与订阅查询匹配的数据。
// Configure and open the realm final config = Configuration.flexibleSync(user, [Car.schema]); final realm = Realm(config); // Add subscription to sync all Car objects in the realm realm.subscriptions.update((mutableSubscriptions) { mutableSubscriptions.add(realm.all<Car>()); }); // Sync all subscriptions await realm.subscriptions.waitForSynchronization();
使用 Realm
在同步 Realm 上读取、写入和监视变更的语法与非同步域的语法相同。 当您使用本地数据时,背景线程会集成、上传和下载变更集。
以下代码创建一个新的 Car
对象并将其写入域:
// Write data to realm and it automatically syncs with Atlas // in the background. realm.write(() { realm.add(Car(ObjectId(), 'Toyota', model: 'Rav 4')); });