配置同步 Realm — Java SDK
先决条件
从客户端访问同步 Realm 之前,您必须:
同步 Realm
同步 Realm 使用Atlas Device Sync在客户端设备和同步数据源中存储数据。 打开同步 Realm 的工作方式与打开本地 Realm 完全相同,只是您使用SyncConfiguration
自定义同步 Realm 的设置。
同步 Realm 配置
要配置 Realm 的设置,请使用 SyncConfiguration.Builder 创建SyncConfiguration 。
要打开同步 Realm,请调用getInstanceAsync()并传入SyncConfiguration对象。
当您的应用程序使用“Flexible Sync”时,请使用 SyncConfiguration.InitialFlexibleSyncSubscriptions() 实例调用 initialSubscriptions() 同步配置构建器方法以打开同步 Realm。在configure()
方法中,使用名称实例化UnmanagedSubscription
,并使用Subscription.create()进行查询。 将新订阅传递给MutableSubscriptionSet参数的add()
方法,将其添加到您的订阅中:
// instantiate a Realm App connection String appID = YOUR_APP_ID; // replace this with your App ID App app = new App(new AppConfiguration.Builder(appID) .build()); // authenticate a user Credentials credentials = Credentials.anonymous(); app.loginAsync(credentials, it -> { if (it.isSuccess()) { User user = it.get(); // add an initial subscription to the sync configuration SyncConfiguration config = new SyncConfiguration.Builder(app.currentUser()) .initialSubscriptions(new SyncConfiguration.InitialFlexibleSyncSubscriptions() { public void configure(Realm realm, MutableSubscriptionSet subscriptions) { subscriptions.add(Subscription.create("subscriptionName", realm.where(Frog.class) .equalTo("species", "spring peeper"))); } }) .build(); // instantiate a realm instance with the flexible sync configuration Realm.getInstanceAsync(config, new Realm.Callback() { public void onSuccess(Realm realm) { Log.v("EXAMPLE", "Successfully opened a realm."); } }); } else { Log.e("EXAMPLE", "Failed to log in: " + it.getError().getErrorMessage()); } });
// instantiate a Realm App connection val appID: String = YOUR_APP_ID // replace this with your App ID val app = App( AppConfiguration.Builder(appID) .build() ) // authenticate a user val credentials = Credentials.anonymous() app.loginAsync( credentials ) { it: App.Result<User?> -> if (it.isSuccess) { val user = it.get() // add an initial subscription to the sync configuration val config = SyncConfiguration.Builder(app.currentUser()) .initialSubscriptions { realm, subscriptions -> subscriptions.add( Subscription.create( "subscriptionName", realm.where(Frog::class.java) .equalTo("species", "spring peeper") ) ) } .build() // instantiate a realm instance with the flexible sync configuration Realm.getInstanceAsync(config, object : Realm.Callback() { override fun onSuccess(realm: Realm) { Log.v("EXAMPLE", "Successfully opened a realm.") } }) } else { Log.e( "EXAMPLE", "Failed to log in: " + it.error.errorMessage ) } }
重要
UI 线程上的同步读写
默认情况下,只能使用异步事务在应用程序的用户界面线程中读取或写入域。也就是说,除非您明确允许使用同步方法,否则您只能在 Android 应用程序的主线程中使用名称以单词 Async
结尾的 Realm
方法。
此限制是为了应用程序用户的利益:在 UI 线程上执行读写操作,可能导致 UI 交互无响应或速度缓慢,所以通常来说,最好以异步方式或在后台线程中处理这些操作。但是,如果应用程序需要在 UI 线程上使用同步 Realm 读取或写入,则可以通过以下 SyncConfiguration
选项明确支持使用同步方法:
SyncConfiguration config = new SyncConfiguration.Builder(app.currentUser()) .allowQueriesOnUiThread(true) .allowWritesOnUiThread(true) .initialSubscriptions(new SyncConfiguration.InitialFlexibleSyncSubscriptions() { public void configure(Realm realm, MutableSubscriptionSet subscriptions) { subscriptions.add(Subscription.create("springPeepers", realm.where(Frog.class) .equalTo("species", "spring peeper"))); } }) .build(); Realm.getInstanceAsync(config, new Realm.Callback() { public void onSuccess(Realm realm) { Log.v( "EXAMPLE", "Successfully opened a realm with reads and writes allowed on the UI thread."); } });
val config = SyncConfiguration.Builder(app.currentUser()) .allowQueriesOnUiThread(true) .allowWritesOnUiThread(true) .initialSubscriptions { realm, subscriptions -> subscriptions.add( Subscription.create( "subscriptionName", realm.where(Frog::class.java) .equalTo("species", "springPeepers") ) ) } .build() Realm.getInstanceAsync(config, object : Realm.Callback() { override fun onSuccess(realm: Realm) { Log.v( "EXAMPLE", "Successfully opened a realm with reads and writes allowed on the UI thread." ) } })
离线时打开同步 Realm
您可以使用与在线打开同步域完全相同的语法在离线时打开同步域 。并非所有 SDK 都遵循这种模式,因此跨平台开发者应查阅每个 SDK 的文档以了解更多信息。
关闭 Realm
请务必记住在使用 Realm 实例完成后调用close()方法以释放资源。 忽视关闭 Realm 可能会导致OutOfMemoryError
。
realm.close();
realm.close()