Configuration

public struct Configuration

A Configuration instance describes the different options used to create an instance of a Realm.

Configuration instances are just plain Swift structs. Unlike Realms and Objects, they can be freely shared between threads as long as you do not mutate them.

Creating configuration values for class subsets (by setting the objectClasses property) can be expensive. Because of this, you will normally want to cache and reuse a single configuration value for each distinct configuration rather than creating a new value each time you open a Realm.

  • The default Configuration used to create Realms when no configuration is explicitly specified (i.e. Realm())

    Declaration

    Swift

    public static var defaultConfiguration: Configuration { get set }
  • Creates a Configuration which can be used to create new Realm instances.

    Note

    The fileURL, inMemoryIdentifier, and syncConfiguration parameters are mutually exclusive. Only set one of them, or none if you wish to use the default file URL.

    Declaration

    Swift

    public init(fileURL: URL? = URL(fileURLWithPath: RLMRealmPathForFile("default.realm"), isDirectory: false),
                inMemoryIdentifier: String? = nil,
                syncConfiguration: SyncConfiguration? = nil,
                encryptionKey: Data? = nil,
                readOnly: Bool = false,
                schemaVersion: UInt64 = 0,
                migrationBlock: MigrationBlock? = nil,
                deleteRealmIfMigrationNeeded: Bool = false,
                shouldCompactOnLaunch: ((Int, Int) -> Bool)? = nil,
                objectTypes: [Object.Type]? = nil)

    Parameters

    fileURL

    The local URL to the Realm file.

    inMemoryIdentifier

    A string used to identify a particular in-memory Realm.

    syncConfiguration

    For Realms intended to sync with the Realm Object Server, a sync configuration.

    encryptionKey

    An optional 64-byte key to use to encrypt the data.

    readOnly

    Whether the Realm is read-only (must be true for read-only files).

    schemaVersion

    The current schema version.

    migrationBlock

    The block which migrates the Realm to the current version.

    deleteRealmIfMigrationNeeded

    If true, recreate the Realm file with the provided schema if a migration is required.

    shouldCompactOnLaunch

    A block called when opening a Realm for the first time during the life of a process to determine if it should be compacted before being returned to the user. It is passed the total file size (data + free space) and the total bytes used by data in the file.

                               Return `true ` to indicate that an attempt to compact the file should be made.
                               The compaction will be skipped if another process is accessing it.
    
    objectTypes

    The subset of Object subclasses persisted in the Realm.

  • A configuration value used to configure a Realm for synchronization with the Realm Object Server. Mutually exclusive with inMemoryIdentifier and fileURL.

    Declaration

    Swift

    public var syncConfiguration: SyncConfiguration? { get set }
  • The local URL of the Realm file. Mutually exclusive with inMemoryIdentifier and syncConfiguration.

    Declaration

    Swift

    public var fileURL: URL? { get set }
  • A string used to identify a particular in-memory Realm. Mutually exclusive with fileURL and syncConfiguration.

    Declaration

    Swift

    public var inMemoryIdentifier: String? { get set }
  • A 64-byte key to use to encrypt the data, or nil if encryption is not enabled.

    Declaration

    Swift

    public var encryptionKey: Data?
  • Whether to open the Realm in read-only mode.

    This is required to be able to open Realm files which are not writeable or are in a directory which is not writeable. This should only be used on files which will not be modified by anyone while they are open, and not just to get a read-only view of a file which may be written to by another thread or process. Opening in read-only mode requires disabling Realm’s reader/writer coordination, so committing a write transaction from another process will result in crashes.

    Declaration

    Swift

    public var readOnly: Bool
  • The current schema version.

    Declaration

    Swift

    public var schemaVersion: UInt64
  • The block which migrates the Realm to the current version.

    Declaration

    Swift

    public var migrationBlock: MigrationBlock?
  • Whether to recreate the Realm file with the provided schema if a migration is required. This is the case when the stored schema differs from the provided schema or the stored schema version differs from the version on this configuration. Setting this property to true deletes the file if a migration would otherwise be required or executed.

    Note

    Setting this property to true doesn’t disable file format migrations.

    Declaration

    Swift

    public var deleteRealmIfMigrationNeeded: Bool
  • A block called when opening a Realm for the first time during the life of a process to determine if it should be compacted before being returned to the user. It is passed the total file size (data + free space) and the total bytes used by data in the file.

    Return true to indicate that an attempt to compact the file should be made. The compaction will be skipped if another process is accessing it.

    Declaration

    Swift

    public var shouldCompactOnLaunch: ((Int, Int) -> Bool)?
  • The classes managed by the Realm.

    Declaration

    Swift

    public var objectTypes: [Object.Type]? { get set }
  • A human-readable description of the configuration value.

    Declaration

    Swift

    public var description: String { get }