Class SyncConfiguration


  • public class SyncConfiguration
    extends RealmConfiguration
    A SyncConfiguration is used to setup a Realm Database that can be synchronized between devices using MongoDB Realm.

    A valid User is required to create a SyncConfiguration. See Credentials and App.loginAsync(Credentials, App.Callback) for more information on how to get a user object.

    A minimal SyncConfiguration can be found below.

     
     App app = new App("app-id");
     User user = app.login(Credentials.anonymous());
     SyncConfiguration config = SyncConfiguration.defaultConfiguration(user, "partition-value");
     Realm realm = Realm.getInstance(config);
     
     

    Synchronized Realms only support additive migrations which can be detected and performed automatically, so the following builder options are not accessible compared to a normal Realm:

    • deleteRealmIfMigrationNeeded()
    • migration(Migration)
    Synchronized Realms are created by using Realm.getInstance(RealmConfiguration) and Realm.getDefaultInstance() like ordinary unsynchronized Realms.
    See Also:
    The docs for more information about synchronization.
    • Method Detail

      • forRecovery

        public static RealmConfiguration forRecovery​(String canonicalPath,
                                                     @Nullable
                                                     byte[] encryptionKey,
                                                     @Nullable
                                                     Object... modules)
        Returns a RealmConfiguration appropriate to open a read-only, non-synced Realm to recover any pending changes. This is useful when trying to open a backup/recovery Realm (after a client reset).
        Parameters:
        canonicalPath - the absolute path to the Realm file defined by this configuration.
        encryptionKey - the key used to encrypt/decrypt the Realm file.
        modules - if specified it will restricts Realm schema to the provided module.
        Returns:
        RealmConfiguration that can be used offline
      • defaultConfig

        public static SyncConfiguration defaultConfig​(User user)
        Returns a default Flexible Sync configuration for the given user.
        Parameters:
        user - The user that will be used for accessing the Realm App.
        Returns:
        the default Flexible Sync configuration for the given user.
      • defaultConfig

        public static SyncConfiguration defaultConfig​(User user,
                                                      @Nullable
                                                      String partitionValue)
        Returns a default Partition-based Sync configuration for the given user and partition value.
        Parameters:
        user - The user that will be used for accessing the Realm App.
        partitionValue - The partition value identifying the remote Realm that will be synchronized.
        Returns:
        the default configuration for the given user and partition value.
      • defaultConfig

        public static SyncConfiguration defaultConfig​(User user,
                                                      @Nullable
                                                      Long partitionValue)
        Returns a default Partition-based Sync configuration for the given user and partition value.
        Parameters:
        user - The user that will be used for accessing the Realm App.
        partitionValue - The partition value identifying the remote Realm that will be synchronized.
        Returns:
        the default configuration for the given user and partition value.
      • defaultConfig

        public static SyncConfiguration defaultConfig​(User user,
                                                      @Nullable
                                                      Integer partitionValue)
        Returns a default Partition-based Sync configuration for the given user and partition value.
        Parameters:
        user - The user that will be used for accessing the Realm App.
        partitionValue - The partition value identifying the remote Realm that will be synchronized.
        Returns:
        the default configuration for the given user and partition value.
      • defaultConfig

        public static SyncConfiguration defaultConfig​(User user,
                                                      @Nullable
                                                      ObjectId partitionValue)
        Returns a default Partition-based Sync configuration for the given user and partition value.
        Parameters:
        user - The user that will be used for accessing the Realm App.
        partitionValue - The partition value identifying the remote Realm that will be synchronized.
        Returns:
        the default configuration for the given user and partition value.
      • defaultConfig

        public static SyncConfiguration defaultConfig​(User user,
                                                      @Nullable
                                                      UUID partitionValue)
        Returns a default Partition-based Sync configuration for the given user and partition value.
        Parameters:
        user - The user that will be used for accessing the Realm App.
        partitionValue - The partition value identifying the remote Realm that will be synchronized.
        Returns:
        the default configuration for the given user and partition value.
      • forRecovery

        public static RealmConfiguration forRecovery​(String canonicalPath)
        Returns a RealmConfiguration appropriate to open a read-only, non-synced Realm to recover any pending changes. This is useful when trying to open a backup/recovery Realm (after a client reset). Note: This will use the default Realm module (composed of all RealmModel), and assume no encryption should be used as well.
        Parameters:
        canonicalPath - the absolute path to the Realm file defined by this configuration.
        Returns:
        RealmConfiguration that can be used offline
      • getInitialSubscriptionsHandler

        public SyncConfiguration.InitialFlexibleSyncSubscriptions getInitialSubscriptionsHandler()
        Returns the configured initial subscription handler for this realm.
        Returns:
        the handler used to configure initial subscriptions for this realm.
      • getUser

        public User getUser()
        Returns the user.
        Returns:
        the user.
      • getServerUrl

        public URI getServerUrl()
        Returns the server URI for the remote MongoDB Realm the local Realm is synchronizing with.
        Returns:
        URI identifying the MongoDB Realm this local Realm is synchronized with.
      • getErrorHandler

        public SyncSession.ErrorHandler getErrorHandler()
        Returns the error handler for this SyncConfiguration.
        Returns:
        the error handler.
      • getSyncClientResetStrategy

        public SyncClientResetStrategy getSyncClientResetStrategy()
        Returns the sync client reset strategy for this SyncConfiguration.
        Returns:
        the sync client reset strategy.
      • shouldDeleteRealmOnLogout

        public boolean shouldDeleteRealmOnLogout()
        Returns true if the Realm file must be deleted once the User owning it logs out.
        Returns:
        true if the Realm file must be deleted if the User logs out. false if the file is allowed to remain behind.
      • shouldWaitForInitialRemoteData

        public boolean shouldWaitForInitialRemoteData()
        Returns true if the Realm will download all known changes from the remote server before being opened the first time.
        Returns:
        true if all remote changes will be downloaded before the Realm can be opened. false if the Realm can be opened immediately.
      • getSessionStopPolicy

        public io.realm.internal.OsRealmConfig.SyncSessionStopPolicy getSessionStopPolicy()
        NOTE: Only for internal usage. May change without warning. Returns the stop policy for the session for this Realm once the Realm has been closed.
        Returns:
        the stop policy used by the session once the Realm is closed.
      • getUrlPrefix

        @Nullable
        public String getUrlPrefix()
        Returns the url prefix used when establishing a sync connection to the Realm Object Server.
      • getPartitionValue

        public BsonValue getPartitionValue()
        Returns the value this Realm is partitioned on. The partition key is a property defined in MongoDB Realm. All classes with a property with this value will be synchronized to the Realm.
        Returns:
        the value being used by MongoDB Realm to partition the server side MongoDB Database into Realms that can be synchronized independently.
        Throws:
        IllegalStateException - if this configuration is for a realm configured for flexible sync. You can use isPartitionBasedSyncConfiguration() before calling this method to check.
      • isFlexibleSyncConfiguration

        public boolean isFlexibleSyncConfiguration()
        Returns whether or not this configuration is for opening a Realm configured for Flexible Sync.
        Returns:
        true if this configuration is for a Flexible Sync Realm, false if not.
      • isPartitionBasedSyncConfiguration

        public boolean isPartitionBasedSyncConfiguration()
        Returns whether or not this configuration is for opening a Realm configured for Partition-based Sync.
        Returns:
        true if this configuration is for a Partition-based Sync Realm, false if not.