123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- import Foundation
- import Realm
- import Realm.Private
- #if !swift(>=4.1)
- fileprivate extension Sequence {
- func compactMap<T>(_ fn: (Self.Iterator.Element) throws -> T?) rethrows -> [T] {
- return try flatMap(fn)
- }
- }
- #endif
- extension Realm {
-
- public struct Configuration {
-
-
- public static var defaultConfiguration: Configuration {
- get {
- return fromRLMRealmConfiguration(RLMRealmConfiguration.default())
- }
- set {
- RLMRealmConfiguration.setDefault(newValue.rlmConfiguration)
- }
- }
-
-
- 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) {
- self.fileURL = fileURL
- if let inMemoryIdentifier = inMemoryIdentifier {
- self.inMemoryIdentifier = inMemoryIdentifier
- }
- if let syncConfiguration = syncConfiguration {
- self.syncConfiguration = syncConfiguration
- }
- self.encryptionKey = encryptionKey
- self.readOnly = readOnly
- self.schemaVersion = schemaVersion
- self.migrationBlock = migrationBlock
- self.deleteRealmIfMigrationNeeded = deleteRealmIfMigrationNeeded
- self.shouldCompactOnLaunch = shouldCompactOnLaunch
- self.objectTypes = objectTypes
- }
-
-
- public var syncConfiguration: SyncConfiguration? {
- set {
- _path = nil
- _inMemoryIdentifier = nil
- _syncConfiguration = newValue
- }
- get {
- return _syncConfiguration
- }
- }
- private var _syncConfiguration: SyncConfiguration?
-
- public var fileURL: URL? {
- set {
- _inMemoryIdentifier = nil
- _syncConfiguration = nil
- _path = newValue?.path
- }
- get {
- return _path.map { URL(fileURLWithPath: $0) }
- }
- }
- private var _path: String?
-
-
- public var inMemoryIdentifier: String? {
- set {
- _path = nil
- _syncConfiguration = nil
- _inMemoryIdentifier = newValue
- }
- get {
- return _inMemoryIdentifier
- }
- }
- private var _inMemoryIdentifier: String?
-
- public var encryptionKey: Data?
-
- public var readOnly: Bool = false
-
- public var schemaVersion: UInt64 = 0
-
- public var migrationBlock: MigrationBlock?
-
- public var deleteRealmIfMigrationNeeded: Bool = false
-
- public var shouldCompactOnLaunch: ((Int, Int) -> Bool)?
-
- public var objectTypes: [Object.Type]? {
- set {
- self.customSchema = newValue.map { RLMSchema(objectClasses: $0) }
- }
- get {
- return self.customSchema.map { $0.objectSchema.compactMap { $0.objectClass as? Object.Type } }
- }
- }
-
- private var customSchema: RLMSchema?
-
- internal var disableFormatUpgrade: Bool = false
-
- internal var rlmConfiguration: RLMRealmConfiguration {
- let configuration = RLMRealmConfiguration()
- if let fileURL = fileURL {
- configuration.fileURL = fileURL
- } else if let inMemoryIdentifier = inMemoryIdentifier {
- configuration.inMemoryIdentifier = inMemoryIdentifier
- } else if let syncConfiguration = syncConfiguration {
- configuration.syncConfiguration = syncConfiguration.asConfig()
- } else {
- fatalError("A Realm Configuration must specify a path or an in-memory identifier.")
- }
- configuration.encryptionKey = self.encryptionKey
- configuration.readOnly = self.readOnly
- configuration.schemaVersion = self.schemaVersion
- configuration.migrationBlock = self.migrationBlock.map { accessorMigrationBlock($0) }
- configuration.deleteRealmIfMigrationNeeded = self.deleteRealmIfMigrationNeeded
- if let shouldCompactOnLaunch = self.shouldCompactOnLaunch {
- configuration.shouldCompactOnLaunch = ObjectiveCSupport.convert(object: shouldCompactOnLaunch)
- } else {
- configuration.shouldCompactOnLaunch = nil
- }
- configuration.setCustomSchemaWithoutCopying(self.customSchema)
- configuration.disableFormatUpgrade = self.disableFormatUpgrade
- return configuration
- }
- internal static func fromRLMRealmConfiguration(_ rlmConfiguration: RLMRealmConfiguration) -> Configuration {
- var configuration = Configuration()
- configuration._path = rlmConfiguration.fileURL?.path
- configuration._inMemoryIdentifier = rlmConfiguration.inMemoryIdentifier
- if let objcSyncConfig = rlmConfiguration.syncConfiguration {
- configuration._syncConfiguration = SyncConfiguration(config: objcSyncConfig)
- } else {
- configuration._syncConfiguration = nil
- }
- configuration.encryptionKey = rlmConfiguration.encryptionKey
- configuration.readOnly = rlmConfiguration.readOnly
- configuration.schemaVersion = rlmConfiguration.schemaVersion
- configuration.migrationBlock = rlmConfiguration.migrationBlock.map { rlmMigration in
- return { migration, schemaVersion in
- rlmMigration(migration.rlmMigration, schemaVersion)
- }
- }
- configuration.deleteRealmIfMigrationNeeded = rlmConfiguration.deleteRealmIfMigrationNeeded
- configuration.shouldCompactOnLaunch = rlmConfiguration.shouldCompactOnLaunch.map(ObjectiveCSupport.convert)
- configuration.customSchema = rlmConfiguration.customSchema
- configuration.disableFormatUpgrade = rlmConfiguration.disableFormatUpgrade
- return configuration
- }
- }
- }
- extension Realm.Configuration: CustomStringConvertible {
-
- public var description: String {
- return gsub(pattern: "\\ARLMRealmConfiguration",
- template: "Realm.Configuration",
- string: rlmConfiguration.description) ?? ""
- }
- }
|