RLMRealmConfiguration.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <Realm/RLMRealm.h>
  19. @class RLMEventConfiguration, RLMSyncConfiguration;
  20. RLM_HEADER_AUDIT_BEGIN(nullability, sendability)
  21. /**
  22. A block called when opening a Realm for the first time during the life
  23. of a process to determine if it should be compacted before being returned
  24. to the user. It is passed the total file size (data + free space) and the total
  25. bytes used by data in the file.
  26. Return `YES` to indicate that an attempt to compact the file should be made.
  27. The compaction will be skipped if another process is accessing it.
  28. */
  29. RLM_SWIFT_SENDABLE
  30. typedef BOOL (^RLMShouldCompactOnLaunchBlock)(NSUInteger totalBytes, NSUInteger bytesUsed);
  31. /**
  32. A block which receives a subscription set instance, that can be used to add an initial set of subscriptions which will be executed
  33. when the Realm is first opened.
  34. */
  35. RLM_SWIFT_SENDABLE
  36. typedef void(^RLMFlexibleSyncInitialSubscriptionsBlock)(RLMSyncSubscriptionSet * _Nonnull subscriptions);
  37. /**
  38. An `RLMRealmConfiguration` instance describes the different options used to
  39. create an instance of a Realm.
  40. `RLMRealmConfiguration` instances are just plain `NSObject`s. Unlike `RLMRealm`s
  41. and `RLMObject`s, they can be freely shared between threads as long as you do not
  42. mutate them.
  43. Creating configuration objects for class subsets (by setting the
  44. `objectClasses` property) can be expensive. Because of this, you will normally want to
  45. cache and reuse a single configuration object for each distinct configuration rather than
  46. creating a new object each time you open a Realm.
  47. */
  48. @interface RLMRealmConfiguration : NSObject<NSCopying>
  49. #pragma mark - Default Configuration
  50. /**
  51. Returns the default configuration used to create Realms when no other
  52. configuration is explicitly specified (i.e. `+[RLMRealm defaultRealm]`).
  53. @return The default Realm configuration.
  54. */
  55. + (instancetype)defaultConfiguration;
  56. /**
  57. Sets the default configuration to the given `RLMRealmConfiguration`.
  58. @param configuration The new default Realm configuration.
  59. */
  60. + (void)setDefaultConfiguration:(RLMRealmConfiguration *)configuration;
  61. #pragma mark - Properties
  62. /// The local URL of the Realm file. Mutually exclusive with `inMemoryIdentifier`;
  63. /// setting one of the two properties will automatically nil out the other.
  64. @property (nonatomic, copy, nullable) NSURL *fileURL;
  65. /// A string used to identify a particular in-memory Realm. Mutually exclusive with `fileURL`,
  66. /// `seedFilePath`and `syncConfiguration`;
  67. /// setting any one of the three properties will automatically nil out the other two.
  68. @property (nonatomic, copy, nullable) NSString *inMemoryIdentifier;
  69. /// A 64-byte key to use to encrypt the data, or `nil` if encryption is not enabled.
  70. @property (nonatomic, copy, nullable) NSData *encryptionKey;
  71. /// Whether to open the Realm in read-only mode.
  72. ///
  73. /// For non-synchronized Realms, this is required to be able to open Realm
  74. /// files which are not writeable or are in a directory which is not writeable.
  75. /// This should only be used on files which will not be modified by anyone
  76. /// while they are open, and not just to get a read-only view of a file which
  77. /// may be written to by another thread or process. Opening in read-only mode
  78. /// requires disabling Realm's reader/writer coordination, so committing a
  79. /// write transaction from another process will result in crashes.
  80. ///
  81. /// Syncronized Realms must always be writeable (as otherwise no
  82. /// synchronization could happen), and this instead merely disallows performing
  83. /// write transactions on the Realm. In addition, it will skip some automatic
  84. /// writes made to the Realm, such as to initialize the Realm's schema. Setting
  85. /// `readOnly = YES` is not strictly required for Realms which the sync user
  86. /// does not have write access to, but is highly recommended as it will improve
  87. /// error reporting and catch some errors earlier.
  88. ///
  89. /// Realms using query-based sync cannot be opened in read-only mode.
  90. @property (nonatomic) BOOL readOnly;
  91. /// The current schema version.
  92. @property (nonatomic) uint64_t schemaVersion;
  93. /// The block which migrates the Realm to the current version.
  94. @property (nonatomic, copy, nullable) RLMMigrationBlock migrationBlock;
  95. /**
  96. Whether to recreate the Realm file with the provided schema if a migration is required.
  97. This is the case when the stored schema differs from the provided schema or
  98. the stored schema version differs from the version on this configuration.
  99. Setting this property to `YES` deletes the file if a migration would otherwise be required or executed.
  100. @note Setting this property to `YES` doesn't disable file format migrations.
  101. */
  102. @property (nonatomic) BOOL deleteRealmIfMigrationNeeded;
  103. /**
  104. A block called when opening a Realm for the first time during the life
  105. of a process to determine if it should be compacted before being returned
  106. to the user. It is passed the total file size (data + free space) and the total
  107. bytes used by data in the file.
  108. Return `YES` to indicate that an attempt to compact the file should be made.
  109. The compaction will be skipped if another process is accessing it.
  110. */
  111. @property (nonatomic, copy, nullable) RLMShouldCompactOnLaunchBlock shouldCompactOnLaunch;
  112. /// The classes managed by the Realm.
  113. @property (nonatomic, copy, nullable) NSArray *objectClasses;
  114. /**
  115. The maximum number of live versions in the Realm file before an exception will
  116. be thrown when attempting to start a write transaction.
  117. Realm provides MVCC snapshot isolation, meaning that writes on one thread do
  118. not overwrite data being read on another thread, and instead write a new copy
  119. of that data. When a Realm refreshes it updates to the latest version of the
  120. data and releases the old versions, allowing them to be overwritten by
  121. subsequent write transactions.
  122. Under normal circumstances this is not a problem, but if the number of active
  123. versions grow too large, it will have a negative effect on the filesize on
  124. disk. This can happen when performing writes on many different threads at
  125. once, when holding on to frozen objects for an extended time, or when
  126. performing long operations on background threads which do not allow the Realm
  127. to refresh.
  128. Setting this property to a non-zero value makes it so that exceeding the set
  129. number of versions will instead throw an exception. This can be used with a
  130. low value during development to help identify places that may be problematic,
  131. or in production use to cause the app to crash rather than produce a Realm
  132. file which is too large to be opened.
  133. */
  134. @property (nonatomic) NSUInteger maximumNumberOfActiveVersions;
  135. /**
  136. When opening the Realm for the first time, instead of creating an empty file,
  137. the Realm file will be copied from the provided seed file path and used instead.
  138. This can be used to open a Realm file with pre-populated data.
  139. If a realm file already exists at the configuration's destination path, the seed file
  140. will not be copied and the already existing realm will be opened instead.
  141. Note that to use this parameter with a synced Realm configuration
  142. the seed Realm must be appropriately copied to a destination with
  143. `[RLMRealm writeCopyForConfiguration:]` first.
  144. This option is mutually exclusive with `inMemoryIdentifier`. Setting a `seedFilePath`
  145. will nil out the `inMemoryIdentifier`.
  146. */
  147. @property (nonatomic, copy, nullable) NSURL *seedFilePath;
  148. /**
  149. A configuration object representing configuration state for Realms intended
  150. to sync with Atlas Device Sync.
  151. This property is mutually exclusive with both `inMemoryIdentifier` and `fileURL`;
  152. setting any one of the three properties will automatically nil out the other two.
  153. @see `RLMSyncConfiguration`
  154. */
  155. @property (nullable, nonatomic) RLMSyncConfiguration *syncConfiguration;
  156. @end
  157. RLM_HEADER_AUDIT_END(nullability, sendability)