RealmConfiguration.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2015 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. import Foundation
  19. import Realm
  20. import Realm.Private
  21. #if !swift(>=4.1)
  22. fileprivate extension Sequence {
  23. func compactMap<T>(_ fn: (Self.Iterator.Element) throws -> T?) rethrows -> [T] {
  24. return try flatMap(fn)
  25. }
  26. }
  27. #endif
  28. extension Realm {
  29. /**
  30. A `Configuration` instance describes the different options used to create an instance of a Realm.
  31. `Configuration` instances are just plain Swift structs. Unlike `Realm`s and `Object`s, they can be freely shared
  32. between threads as long as you do not mutate them.
  33. Creating configuration values for class subsets (by setting the `objectClasses` property) can be expensive. Because
  34. of this, you will normally want to cache and reuse a single configuration value for each distinct configuration
  35. rather than creating a new value each time you open a Realm.
  36. */
  37. public struct Configuration {
  38. // MARK: Default Configuration
  39. /**
  40. The default `Configuration` used to create Realms when no configuration is explicitly specified (i.e.
  41. `Realm()`)
  42. */
  43. public static var defaultConfiguration: Configuration {
  44. get {
  45. return fromRLMRealmConfiguration(RLMRealmConfiguration.default())
  46. }
  47. set {
  48. RLMRealmConfiguration.setDefault(newValue.rlmConfiguration)
  49. }
  50. }
  51. // MARK: Initialization
  52. /**
  53. Creates a `Configuration` which can be used to create new `Realm` instances.
  54. - note: The `fileURL`, `inMemoryIdentifier`, and `syncConfiguration` parameters are mutually exclusive. Only
  55. set one of them, or none if you wish to use the default file URL.
  56. - parameter fileURL: The local URL to the Realm file.
  57. - parameter inMemoryIdentifier: A string used to identify a particular in-memory Realm.
  58. - parameter syncConfiguration: For Realms intended to sync with the Realm Object Server, a sync configuration.
  59. - parameter encryptionKey: An optional 64-byte key to use to encrypt the data.
  60. - parameter readOnly: Whether the Realm is read-only (must be true for read-only files).
  61. - parameter schemaVersion: The current schema version.
  62. - parameter migrationBlock: The block which migrates the Realm to the current version.
  63. - parameter deleteRealmIfMigrationNeeded: If `true`, recreate the Realm file with the provided
  64. schema if a migration is required.
  65. - parameter shouldCompactOnLaunch: A block called when opening a Realm for the first time during the
  66. life of a process to determine if it should be compacted before being
  67. returned to the user. It is passed the total file size (data + free space)
  68. and the total bytes used by data in the file.
  69. Return `true ` to indicate that an attempt to compact the file should be made.
  70. The compaction will be skipped if another process is accessing it.
  71. - parameter objectTypes: The subset of `Object` subclasses persisted in the Realm.
  72. */
  73. public init(fileURL: URL? = URL(fileURLWithPath: RLMRealmPathForFile("default.realm"), isDirectory: false),
  74. inMemoryIdentifier: String? = nil,
  75. syncConfiguration: SyncConfiguration? = nil,
  76. encryptionKey: Data? = nil,
  77. readOnly: Bool = false,
  78. schemaVersion: UInt64 = 0,
  79. migrationBlock: MigrationBlock? = nil,
  80. deleteRealmIfMigrationNeeded: Bool = false,
  81. shouldCompactOnLaunch: ((Int, Int) -> Bool)? = nil,
  82. objectTypes: [Object.Type]? = nil) {
  83. self.fileURL = fileURL
  84. if let inMemoryIdentifier = inMemoryIdentifier {
  85. self.inMemoryIdentifier = inMemoryIdentifier
  86. }
  87. if let syncConfiguration = syncConfiguration {
  88. self.syncConfiguration = syncConfiguration
  89. }
  90. self.encryptionKey = encryptionKey
  91. self.readOnly = readOnly
  92. self.schemaVersion = schemaVersion
  93. self.migrationBlock = migrationBlock
  94. self.deleteRealmIfMigrationNeeded = deleteRealmIfMigrationNeeded
  95. self.shouldCompactOnLaunch = shouldCompactOnLaunch
  96. self.objectTypes = objectTypes
  97. }
  98. // MARK: Configuration Properties
  99. /**
  100. A configuration value used to configure a Realm for synchronization with the Realm Object Server. Mutually
  101. exclusive with `inMemoryIdentifier`.
  102. */
  103. public var syncConfiguration: SyncConfiguration? {
  104. set {
  105. _inMemoryIdentifier = nil
  106. _syncConfiguration = newValue
  107. }
  108. get {
  109. return _syncConfiguration
  110. }
  111. }
  112. private var _syncConfiguration: SyncConfiguration?
  113. /// The local URL of the Realm file. Mutually exclusive with `inMemoryIdentifier`.
  114. public var fileURL: URL? {
  115. set {
  116. _inMemoryIdentifier = nil
  117. _path = newValue?.path
  118. }
  119. get {
  120. return _path.map { URL(fileURLWithPath: $0) }
  121. }
  122. }
  123. private var _path: String?
  124. /// A string used to identify a particular in-memory Realm. Mutually exclusive with `fileURL` and
  125. /// `syncConfiguration`.
  126. public var inMemoryIdentifier: String? {
  127. set {
  128. _path = nil
  129. _syncConfiguration = nil
  130. _inMemoryIdentifier = newValue
  131. }
  132. get {
  133. return _inMemoryIdentifier
  134. }
  135. }
  136. private var _inMemoryIdentifier: String?
  137. /// A 64-byte key to use to encrypt the data, or `nil` if encryption is not enabled.
  138. public var encryptionKey: Data?
  139. /**
  140. Whether to open the Realm in read-only mode.
  141. This is required to be able to open Realm files which are not writeable or are in a directory which is not
  142. writeable. This should only be used on files which will not be modified by anyone while they are open, and not
  143. just to get a read-only view of a file which may be written to by another thread or process. Opening in
  144. read-only mode requires disabling Realm's reader/writer coordination, so committing a write transaction from
  145. another process will result in crashes.
  146. */
  147. public var readOnly: Bool = false
  148. /// The current schema version.
  149. public var schemaVersion: UInt64 = 0
  150. /// The block which migrates the Realm to the current version.
  151. public var migrationBlock: MigrationBlock?
  152. /**
  153. Whether to recreate the Realm file with the provided schema if a migration is required. This is the case when
  154. the stored schema differs from the provided schema or the stored schema version differs from the version on
  155. this configuration. Setting this property to `true` deletes the file if a migration would otherwise be required
  156. or executed.
  157. - note: Setting this property to `true` doesn't disable file format migrations.
  158. */
  159. public var deleteRealmIfMigrationNeeded: Bool = false
  160. /**
  161. A block called when opening a Realm for the first time during the
  162. life of a process to determine if it should be compacted before being
  163. returned to the user. It is passed the total file size (data + free space)
  164. and the total bytes used by data in the file.
  165. Return `true ` to indicate that an attempt to compact the file should be made.
  166. The compaction will be skipped if another process is accessing it.
  167. */
  168. public var shouldCompactOnLaunch: ((Int, Int) -> Bool)?
  169. /// The classes managed by the Realm.
  170. public var objectTypes: [Object.Type]? {
  171. set {
  172. self.customSchema = newValue.map { RLMSchema(objectClasses: $0) }
  173. }
  174. get {
  175. return self.customSchema.map { $0.objectSchema.compactMap { $0.objectClass as? Object.Type } }
  176. }
  177. }
  178. /// A custom schema to use for the Realm.
  179. private var customSchema: RLMSchema?
  180. /// If `true`, disables automatic format upgrades when accessing the Realm.
  181. internal var disableFormatUpgrade: Bool = false
  182. // MARK: Private Methods
  183. internal var rlmConfiguration: RLMRealmConfiguration {
  184. let configuration = RLMRealmConfiguration()
  185. if let syncConfiguration = syncConfiguration {
  186. configuration.syncConfiguration = syncConfiguration.asConfig()
  187. }
  188. if let fileURL = fileURL {
  189. configuration.fileURL = fileURL
  190. } else if let inMemoryIdentifier = inMemoryIdentifier {
  191. configuration.inMemoryIdentifier = inMemoryIdentifier
  192. } else if syncConfiguration == nil {
  193. fatalError("A Realm Configuration must specify a path or an in-memory identifier.")
  194. }
  195. configuration.encryptionKey = self.encryptionKey
  196. configuration.readOnly = self.readOnly
  197. configuration.schemaVersion = self.schemaVersion
  198. configuration.migrationBlock = self.migrationBlock.map { accessorMigrationBlock($0) }
  199. configuration.deleteRealmIfMigrationNeeded = self.deleteRealmIfMigrationNeeded
  200. if let shouldCompactOnLaunch = self.shouldCompactOnLaunch {
  201. configuration.shouldCompactOnLaunch = ObjectiveCSupport.convert(object: shouldCompactOnLaunch)
  202. } else {
  203. configuration.shouldCompactOnLaunch = nil
  204. }
  205. configuration.setCustomSchemaWithoutCopying(self.customSchema)
  206. configuration.disableFormatUpgrade = self.disableFormatUpgrade
  207. return configuration
  208. }
  209. internal static func fromRLMRealmConfiguration(_ rlmConfiguration: RLMRealmConfiguration) -> Configuration {
  210. var configuration = Configuration()
  211. configuration._path = rlmConfiguration.fileURL?.path
  212. configuration._inMemoryIdentifier = rlmConfiguration.inMemoryIdentifier
  213. if let objcSyncConfig = rlmConfiguration.syncConfiguration {
  214. configuration._syncConfiguration = SyncConfiguration(config: objcSyncConfig)
  215. } else {
  216. configuration._syncConfiguration = nil
  217. }
  218. configuration.encryptionKey = rlmConfiguration.encryptionKey
  219. configuration.readOnly = rlmConfiguration.readOnly
  220. configuration.schemaVersion = rlmConfiguration.schemaVersion
  221. configuration.migrationBlock = rlmConfiguration.migrationBlock.map { rlmMigration in
  222. return { migration, schemaVersion in
  223. rlmMigration(migration.rlmMigration, schemaVersion)
  224. }
  225. }
  226. configuration.deleteRealmIfMigrationNeeded = rlmConfiguration.deleteRealmIfMigrationNeeded
  227. configuration.shouldCompactOnLaunch = rlmConfiguration.shouldCompactOnLaunch.map(ObjectiveCSupport.convert)
  228. configuration.customSchema = rlmConfiguration.customSchema
  229. configuration.disableFormatUpgrade = rlmConfiguration.disableFormatUpgrade
  230. return configuration
  231. }
  232. }
  233. }
  234. // MARK: CustomStringConvertible
  235. extension Realm.Configuration: CustomStringConvertible {
  236. /// A human-readable description of the configuration value.
  237. public var description: String {
  238. return gsub(pattern: "\\ARLMRealmConfiguration",
  239. template: "Realm.Configuration",
  240. string: rlmConfiguration.description) ?? ""
  241. }
  242. }