RLMRealmConfiguration.h 6.8 KB

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