RLMRealmConfiguration.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. @property (nonatomic, copy, nullable) NSURL *fileURL;
  57. /// A string used to identify a particular in-memory Realm. Mutually exclusive with `fileURL`.
  58. @property (nonatomic, copy, nullable) NSString *inMemoryIdentifier;
  59. /// A 64-byte key to use to encrypt the data, or `nil` if encryption is not enabled.
  60. @property (nonatomic, copy, nullable) NSData *encryptionKey;
  61. /// Whether to open the Realm in read-only mode.
  62. ///
  63. /// This is required to be able to open Realm files which are not writeable or
  64. /// are in a directory which is not writeable. This should only be used on files
  65. /// which will not be modified by anyone while they are open, and not just to
  66. /// get a read-only view of a file which may be written to by another thread or
  67. /// process. Opening in read-only mode requires disabling Realm's reader/writer
  68. /// coordination, so committing a write transaction from another process will
  69. /// result in crashes.
  70. @property (nonatomic) BOOL readOnly;
  71. /// The current schema version.
  72. @property (nonatomic) uint64_t schemaVersion;
  73. /// The block which migrates the Realm to the current version.
  74. @property (nonatomic, copy, nullable) RLMMigrationBlock migrationBlock;
  75. /**
  76. Whether to recreate the Realm file with the provided schema if a migration is required.
  77. This is the case when the stored schema differs from the provided schema or
  78. the stored schema version differs from the version on this configuration.
  79. Setting this property to `YES` deletes the file if a migration would otherwise be required or executed.
  80. @note Setting this property to `YES` doesn't disable file format migrations.
  81. */
  82. @property (nonatomic) BOOL deleteRealmIfMigrationNeeded;
  83. /**
  84. A block called when opening a Realm for the first time during the life
  85. of a process to determine if it should be compacted before being returned
  86. to the user. It is passed the total file size (data + free space) and the total
  87. bytes used by data in the file.
  88. Return `YES` to indicate that an attempt to compact the file should be made.
  89. The compaction will be skipped if another process is accessing it.
  90. */
  91. @property (nonatomic, copy, nullable) RLMShouldCompactOnLaunchBlock shouldCompactOnLaunch;
  92. /// The classes managed by the Realm.
  93. @property (nonatomic, copy, nullable) NSArray *objectClasses;
  94. @end
  95. NS_ASSUME_NONNULL_END