RLMRealm.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2014 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 "RLMConstants.h"
  20. @class RLMRealmConfiguration, RLMRealm, RLMObject, RLMSchema, RLMMigration, RLMNotificationToken, RLMThreadSafeReference, RLMAsyncOpenTask;
  21. struct RLMRealmPrivileges;
  22. struct RLMClassPrivileges;
  23. struct RLMObjectPrivileges;
  24. /**
  25. A callback block for opening Realms asynchronously.
  26. Returns the Realm if the open was successful, or an error otherwise.
  27. */
  28. typedef void(^RLMAsyncOpenRealmCallback)(RLMRealm * _Nullable realm, NSError * _Nullable error);
  29. NS_ASSUME_NONNULL_BEGIN
  30. /**
  31. An `RLMRealm` instance (also referred to as "a Realm") represents a Realm
  32. database.
  33. Realms can either be stored on disk (see `+[RLMRealm realmWithURL:]`) or in
  34. memory (see `RLMRealmConfiguration`).
  35. `RLMRealm` instances are cached internally, and constructing equivalent `RLMRealm`
  36. objects (for example, by using the same path or identifier) multiple times on a single thread
  37. within a single iteration of the run loop will normally return the same
  38. `RLMRealm` object.
  39. If you specifically want to ensure an `RLMRealm` instance is
  40. destroyed (for example, if you wish to open a Realm, check some property, and
  41. then possibly delete the Realm file and re-open it), place the code which uses
  42. the Realm within an `@autoreleasepool {}` and ensure you have no other
  43. strong references to it.
  44. @warning `RLMRealm` instances are not thread safe and cannot be shared across
  45. threads or dispatch queues. Trying to do so will cause an exception to be thrown.
  46. You must call this method on each thread you want
  47. to interact with the Realm on. For dispatch queues, this means that you must
  48. call it in each block which is dispatched, as a queue is not guaranteed to run
  49. all of its blocks on the same thread.
  50. */
  51. @interface RLMRealm : NSObject
  52. #pragma mark - Creating & Initializing a Realm
  53. /**
  54. Obtains an instance of the default Realm.
  55. The default Realm is used by the `RLMObject` class methods
  56. which do not take an `RLMRealm` parameter, but is otherwise not special. The
  57. default Realm is persisted as *default.realm* under the *Documents* directory of
  58. your Application on iOS, and in your application's *Application Support*
  59. directory on OS X.
  60. The default Realm is created using the default `RLMRealmConfiguration`, which
  61. can be changed via `+[RLMRealmConfiguration setDefaultConfiguration:]`.
  62. @return The default `RLMRealm` instance for the current thread.
  63. */
  64. + (instancetype)defaultRealm;
  65. /**
  66. Obtains an `RLMRealm` instance with the given configuration.
  67. @param configuration A configuration object to use when creating the Realm.
  68. @param error If an error occurs, upon return contains an `NSError` object
  69. that describes the problem. If you are not interested in
  70. possible errors, pass in `NULL`.
  71. @return An `RLMRealm` instance.
  72. */
  73. + (nullable instancetype)realmWithConfiguration:(RLMRealmConfiguration *)configuration error:(NSError **)error;
  74. /**
  75. Obtains an `RLMRealm` instance persisted at a specified file URL.
  76. @param fileURL The local URL of the file the Realm should be saved at.
  77. @return An `RLMRealm` instance.
  78. */
  79. + (instancetype)realmWithURL:(NSURL *)fileURL;
  80. /**
  81. Asynchronously open a Realm and deliver it to a block on the given queue.
  82. Opening a Realm asynchronously will perform all work needed to get the Realm to
  83. a usable state (such as running potentially time-consuming migrations) on a
  84. background thread before dispatching to the given queue. In addition,
  85. synchronized Realms wait for all remote content available at the time the
  86. operation began to be downloaded and available locally.
  87. @param configuration A configuration object to use when opening the Realm.
  88. @param callbackQueue The dispatch queue on which the callback should be run.
  89. @param callback A callback block. If the Realm was successfully opened,
  90. it will be passed in as an argument.
  91. Otherwise, an `NSError` describing what went wrong will be
  92. passed to the block instead.
  93. @note The returned Realm is confined to the thread on which it was created.
  94. Because GCD does not guarantee that queues will always use the same
  95. thread, accessing the returned Realm outside the callback block (even if
  96. accessed from `callbackQueue`) is unsafe.
  97. */
  98. + (RLMAsyncOpenTask *)asyncOpenWithConfiguration:(RLMRealmConfiguration *)configuration
  99. callbackQueue:(dispatch_queue_t)callbackQueue
  100. callback:(RLMAsyncOpenRealmCallback)callback;
  101. /**
  102. The `RLMSchema` used by the Realm.
  103. */
  104. @property (nonatomic, readonly) RLMSchema *schema;
  105. /**
  106. Indicates if the Realm is currently engaged in a write transaction.
  107. @warning Do not simply check this property and then start a write transaction whenever an object needs to be
  108. created, updated, or removed. Doing so might cause a large number of write transactions to be created,
  109. degrading performance. Instead, always prefer performing multiple updates during a single transaction.
  110. */
  111. @property (nonatomic, readonly) BOOL inWriteTransaction;
  112. /**
  113. The `RLMRealmConfiguration` object that was used to create this `RLMRealm` instance.
  114. */
  115. @property (nonatomic, readonly) RLMRealmConfiguration *configuration;
  116. /**
  117. Indicates if this Realm contains any objects.
  118. */
  119. @property (nonatomic, readonly) BOOL isEmpty;
  120. #pragma mark - Notifications
  121. /**
  122. The type of a block to run whenever the data within the Realm is modified.
  123. @see `-[RLMRealm addNotificationBlock:]`
  124. */
  125. typedef void (^RLMNotificationBlock)(RLMNotification notification, RLMRealm *realm);
  126. #pragma mark - Receiving Notification when a Realm Changes
  127. /**
  128. Adds a notification handler for changes in this Realm, and returns a notification token.
  129. Notification handlers are called after each write transaction is committed,
  130. either on the current thread or other threads.
  131. Handler blocks are called on the same thread that they were added on, and may
  132. only be added on threads which are currently within a run loop. Unless you are
  133. specifically creating and running a run loop on a background thread, this will
  134. normally only be the main thread.
  135. The block has the following definition:
  136. typedef void(^RLMNotificationBlock)(RLMNotification notification, RLMRealm *realm);
  137. It receives the following parameters:
  138. - `NSString` \***notification**: The name of the incoming notification. See
  139. `RLMRealmNotification` for information on what
  140. notifications are sent.
  141. - `RLMRealm` \***realm**: The Realm for which this notification occurred.
  142. @param block A block which is called to process Realm notifications.
  143. @return A token object which must be retained as long as you wish to continue
  144. receiving change notifications.
  145. */
  146. - (RLMNotificationToken *)addNotificationBlock:(RLMNotificationBlock)block __attribute__((warn_unused_result));
  147. #pragma mark - Transactions
  148. #pragma mark - Writing to a Realm
  149. /**
  150. Begins a write transaction on the Realm.
  151. Only one write transaction can be open at a time for each Realm file. Write
  152. transactions cannot be nested, and trying to begin a write transaction on a
  153. Realm which is already in a write transaction will throw an exception. Calls to
  154. `beginWriteTransaction` from `RLMRealm` instances for the same Realm file in
  155. other threads or other processes will block until the current write transaction
  156. completes or is cancelled.
  157. Before beginning the write transaction, `beginWriteTransaction` updates the
  158. `RLMRealm` instance to the latest Realm version, as if `refresh` had been
  159. called, and generates notifications if applicable. This has no effect if the
  160. Realm was already up to date.
  161. It is rarely a good idea to have write transactions span multiple cycles of
  162. the run loop, but if you do wish to do so you will need to ensure that the
  163. Realm participating in the write transaction is kept alive until the write
  164. transaction is committed.
  165. */
  166. - (void)beginWriteTransaction;
  167. /**
  168. Commits all write operations in the current write transaction, and ends the
  169. transaction.
  170. After saving the changes, all notification blocks registered on this specific
  171. `RLMRealm` instance are invoked synchronously. Notification blocks registered
  172. on other threads or on collections are invoked asynchronously. If you do not
  173. want to receive a specific notification for this write tranaction, see
  174. `commitWriteTransactionWithoutNotifying:error:`.
  175. This method can fail if there is insufficient disk space available to save the
  176. writes made, or due to unexpected i/o errors. This version of the method throws
  177. an exception when errors occur. Use the version with a `NSError` out parameter
  178. instead if you wish to handle errors.
  179. @warning This method may only be called during a write transaction.
  180. */
  181. - (void)commitWriteTransaction NS_SWIFT_UNAVAILABLE("");
  182. /**
  183. Commits all write operations in the current write transaction, and ends the
  184. transaction.
  185. After saving the changes, all notification blocks registered on this specific
  186. `RLMRealm` instance are invoked synchronously. Notification blocks registered
  187. on other threads or on collections are invoked asynchronously. If you do not
  188. want to receive a specific notification for this write tranaction, see
  189. `commitWriteTransactionWithoutNotifying:error:`.
  190. This method can fail if there is insufficient disk space available to save the
  191. writes made, or due to unexpected i/o errors.
  192. @warning This method may only be called during a write transaction.
  193. @param error If an error occurs, upon return contains an `NSError` object
  194. that describes the problem. If you are not interested in
  195. possible errors, pass in `NULL`.
  196. @return Whether the transaction succeeded.
  197. */
  198. - (BOOL)commitWriteTransaction:(NSError **)error;
  199. /**
  200. Commits all write operations in the current write transaction, without
  201. notifying specific notification blocks of the changes.
  202. After saving the changes, all notification blocks registered on this specific
  203. `RLMRealm` instance are invoked synchronously. Notification blocks registered
  204. on other threads or on collections are scheduled to be invoked asynchronously.
  205. You can skip notifiying specific notification blocks about the changes made
  206. in this write transaction by passing in their associated notification tokens.
  207. This is primarily useful when the write transaction is saving changes already
  208. made in the UI and you do not want to have the notification block attempt to
  209. re-apply the same changes.
  210. The tokens passed to this method must be for notifications for this specific
  211. `RLMRealm` instance. Notifications for different threads cannot be skipped
  212. using this method.
  213. This method can fail if there is insufficient disk space available to save the
  214. writes made, or due to unexpected i/o errors.
  215. @warning This method may only be called during a write transaction.
  216. @param tokens An array of notification tokens which were returned from adding
  217. callbacks which you do not want to be notified for the changes
  218. made in this write transaction.
  219. @param error If an error occurs, upon return contains an `NSError` object
  220. that describes the problem. If you are not interested in
  221. possible errors, pass in `NULL`.
  222. @return Whether the transaction succeeded.
  223. */
  224. - (BOOL)commitWriteTransactionWithoutNotifying:(NSArray<RLMNotificationToken *> *)tokens error:(NSError **)error;
  225. /**
  226. Reverts all writes made during the current write transaction and ends the transaction.
  227. This rolls back all objects in the Realm to the state they were in at the
  228. beginning of the write transaction, and then ends the transaction.
  229. This restores the data for deleted objects, but does not revive invalidated
  230. object instances. Any `RLMObject`s which were added to the Realm will be
  231. invalidated rather than becoming unmanaged.
  232. Given the following code:
  233. ObjectType *oldObject = [[ObjectType objectsWhere:@"..."] firstObject];
  234. ObjectType *newObject = [[ObjectType alloc] init];
  235. [realm beginWriteTransaction];
  236. [realm addObject:newObject];
  237. [realm deleteObject:oldObject];
  238. [realm cancelWriteTransaction];
  239. Both `oldObject` and `newObject` will return `YES` for `isInvalidated`,
  240. but re-running the query which provided `oldObject` will once again return
  241. the valid object.
  242. KVO observers on any objects which were modified during the transaction will
  243. be notified about the change back to their initial values, but no other
  244. notifcations are produced by a cancelled write transaction.
  245. @warning This method may only be called during a write transaction.
  246. */
  247. - (void)cancelWriteTransaction;
  248. /**
  249. Performs actions contained within the given block inside a write transaction.
  250. @see `[RLMRealm transactionWithBlock:error:]`
  251. */
  252. - (void)transactionWithBlock:(__attribute__((noescape)) void(^)(void))block NS_SWIFT_UNAVAILABLE("");
  253. /**
  254. Performs actions contained within the given block inside a write transaction.
  255. Write transactions cannot be nested, and trying to execute a write transaction
  256. on a Realm which is already participating in a write transaction will throw an
  257. exception. Calls to `transactionWithBlock:` from `RLMRealm` instances in other
  258. threads will block until the current write transaction completes.
  259. Before beginning the write transaction, `transactionWithBlock:` updates the
  260. `RLMRealm` instance to the latest Realm version, as if `refresh` had been called, and
  261. generates notifications if applicable. This has no effect if the Realm
  262. was already up to date.
  263. @param block The block containing actions to perform.
  264. @param error If an error occurs, upon return contains an `NSError` object
  265. that describes the problem. If you are not interested in
  266. possible errors, pass in `NULL`.
  267. @return Whether the transaction succeeded.
  268. */
  269. - (BOOL)transactionWithBlock:(__attribute__((noescape)) void(^)(void))block error:(NSError **)error;
  270. /**
  271. Updates the Realm and outstanding objects managed by the Realm to point to the
  272. most recent data.
  273. If the version of the Realm is actually changed, Realm and collection
  274. notifications will be sent to reflect the changes. This may take some time, as
  275. collection notifications are prepared on a background thread. As a result,
  276. calling this method on the main thread is not advisable.
  277. @return Whether there were any updates for the Realm. Note that `YES` may be
  278. returned even if no data actually changed.
  279. */
  280. - (BOOL)refresh;
  281. /**
  282. Set this property to `YES` to automatically update this Realm when changes
  283. happen in other threads.
  284. If set to `YES` (the default), changes made on other threads will be reflected
  285. in this Realm on the next cycle of the run loop after the changes are
  286. committed. If set to `NO`, you must manually call `-refresh` on the Realm to
  287. update it to get the latest data.
  288. Note that by default, background threads do not have an active run loop and you
  289. will need to manually call `-refresh` in order to update to the latest version,
  290. even if `autorefresh` is set to `YES`.
  291. Even with this property enabled, you can still call `-refresh` at any time to
  292. update the Realm before the automatic refresh would occur.
  293. Write transactions will still always advance a Realm to the latest version and
  294. produce local notifications on commit even if autorefresh is disabled.
  295. Disabling `autorefresh` on a Realm without any strong references to it will not
  296. have any effect, and `autorefresh` will revert back to `YES` the next time the
  297. Realm is created. This is normally irrelevant as it means that there is nothing
  298. to refresh (as managed `RLMObject`s, `RLMArray`s, and `RLMResults` have strong
  299. references to the Realm that manages them), but it means that setting
  300. `RLMRealm.defaultRealm.autorefresh = NO` in
  301. `application:didFinishLaunchingWithOptions:` and only later storing Realm
  302. objects will not work.
  303. Defaults to `YES`.
  304. */
  305. @property (nonatomic) BOOL autorefresh;
  306. /**
  307. Writes a compacted and optionally encrypted copy of the Realm to the given local URL.
  308. The destination file cannot already exist.
  309. Note that if this method is called from within a write transaction, the
  310. *current* data is written, not the data from the point when the previous write
  311. transaction was committed.
  312. @param fileURL Local URL to save the Realm to.
  313. @param key Optional 64-byte encryption key to encrypt the new file with.
  314. @param error If an error occurs, upon return contains an `NSError` object
  315. that describes the problem. If you are not interested in
  316. possible errors, pass in `NULL`.
  317. @return `YES` if the Realm was successfully written to disk, `NO` if an error occurred.
  318. */
  319. - (BOOL)writeCopyToURL:(NSURL *)fileURL encryptionKey:(nullable NSData *)key error:(NSError **)error;
  320. /**
  321. Invalidates all `RLMObject`s, `RLMResults`, `RLMLinkingObjects`, and `RLMArray`s managed by the Realm.
  322. A Realm holds a read lock on the version of the data accessed by it, so
  323. that changes made to the Realm on different threads do not modify or delete the
  324. data seen by this Realm. Calling this method releases the read lock,
  325. allowing the space used on disk to be reused by later write transactions rather
  326. than growing the file. This method should be called before performing long
  327. blocking operations on a background thread on which you previously read data
  328. from the Realm which you no longer need.
  329. All `RLMObject`, `RLMResults` and `RLMArray` instances obtained from this
  330. `RLMRealm` instance on the current thread are invalidated. `RLMObject`s and `RLMArray`s
  331. cannot be used. `RLMResults` will become empty. The Realm itself remains valid,
  332. and a new read transaction is implicitly begun the next time data is read from the Realm.
  333. Calling this method multiple times in a row without reading any data from the
  334. Realm, or before ever reading any data from the Realm, is a no-op. This method
  335. may not be called on a read-only Realm.
  336. */
  337. - (void)invalidate;
  338. #pragma mark - Accessing Objects
  339. /**
  340. Returns the same object as the one referenced when the `RLMThreadSafeReference` was first created,
  341. but resolved for the current Realm for this thread. Returns `nil` if this object was deleted after
  342. the reference was created.
  343. @param reference The thread-safe reference to the thread-confined object to resolve in this Realm.
  344. @warning A `RLMThreadSafeReference` object must be resolved at most once.
  345. Failing to resolve a `RLMThreadSafeReference` will result in the source version of the
  346. Realm being pinned until the reference is deallocated.
  347. An exception will be thrown if a reference is resolved more than once.
  348. @warning Cannot call within a write transaction.
  349. @note Will refresh this Realm if the source Realm was at a later version than this one.
  350. @see `+[RLMThreadSafeReference referenceWithThreadConfined:]`
  351. */
  352. - (nullable id)resolveThreadSafeReference:(RLMThreadSafeReference *)reference
  353. NS_REFINED_FOR_SWIFT;
  354. #pragma mark - Adding and Removing Objects from a Realm
  355. /**
  356. Adds an object to the Realm.
  357. Once added, this object is considered to be managed by the Realm. It can be retrieved
  358. using the `objectsWhere:` selectors on `RLMRealm` and on subclasses of `RLMObject`.
  359. When added, all child relationships referenced by this object will also be added to
  360. the Realm if they are not already in it.
  361. If the object or any related objects are already being managed by a different Realm
  362. an exception will be thrown. Use `-[RLMObject createInRealm:withObject:]` to insert a copy of a managed object
  363. into a different Realm.
  364. The object to be added must be valid and cannot have been previously deleted
  365. from a Realm (i.e. `isInvalidated` must be `NO`).
  366. @warning This method may only be called during a write transaction.
  367. @param object The object to be added to this Realm.
  368. */
  369. - (void)addObject:(RLMObject *)object;
  370. /**
  371. Adds all the objects in a collection to the Realm.
  372. This is the equivalent of calling `addObject:` for every object in a collection.
  373. @warning This method may only be called during a write transaction.
  374. @param objects An enumerable collection such as `NSArray`, `RLMArray`, or `RLMResults`,
  375. containing Realm objects to be added to the Realm.
  376. @see `addObject:`
  377. */
  378. - (void)addObjects:(id<NSFastEnumeration>)objects;
  379. /**
  380. Adds or updates an existing object into the Realm.
  381. The object provided must have a designated primary key. If no objects exist in the Realm
  382. with the same primary key value, the object is inserted. Otherwise, the existing object is
  383. updated with any changed values.
  384. As with `addObject:`, the object cannot already be managed by a different
  385. Realm. Use `-[RLMObject createOrUpdateInRealm:withValue:]` to copy values to
  386. a different Realm.
  387. If there is a property or KVC value on `object` whose value is nil, and it corresponds
  388. to a nullable property on an existing object being updated, that nullable property will
  389. be set to nil.
  390. @warning This method may only be called during a write transaction.
  391. @param object The object to be added or updated.
  392. */
  393. - (void)addOrUpdateObject:(RLMObject *)object;
  394. /**
  395. Adds or updates all the objects in a collection into the Realm.
  396. This is the equivalent of calling `addOrUpdateObject:` for every object in a collection.
  397. @warning This method may only be called during a write transaction.
  398. @param objects An enumerable collection such as `NSArray`, `RLMArray`, or `RLMResults`,
  399. containing Realm objects to be added to or updated within the Realm.
  400. @see `addOrUpdateObject:`
  401. */
  402. - (void)addOrUpdateObjects:(id<NSFastEnumeration>)objects;
  403. /**
  404. Deletes an object from the Realm. Once the object is deleted it is considered invalidated.
  405. @warning This method may only be called during a write transaction.
  406. @param object The object to be deleted.
  407. */
  408. - (void)deleteObject:(RLMObject *)object;
  409. /**
  410. Deletes one or more objects from the Realm.
  411. This is the equivalent of calling `deleteObject:` for every object in a collection.
  412. @warning This method may only be called during a write transaction.
  413. @param objects An enumerable collection such as `NSArray`, `RLMArray`, or `RLMResults`,
  414. containing objects to be deleted from the Realm.
  415. @see `deleteObject:`
  416. */
  417. - (void)deleteObjects:(id<NSFastEnumeration>)objects;
  418. /**
  419. Deletes all objects from the Realm.
  420. @warning This method may only be called during a write transaction.
  421. @see `deleteObject:`
  422. */
  423. - (void)deleteAllObjects;
  424. #pragma mark - Migrations
  425. /**
  426. The type of a migration block used to migrate a Realm.
  427. @param migration A `RLMMigration` object used to perform the migration. The
  428. migration object allows you to enumerate and alter any
  429. existing objects which require migration.
  430. @param oldSchemaVersion The schema version of the Realm being migrated.
  431. */
  432. typedef void (^RLMMigrationBlock)(RLMMigration *migration, uint64_t oldSchemaVersion);
  433. /**
  434. Returns the schema version for a Realm at a given local URL.
  435. @param fileURL Local URL to a Realm file.
  436. @param key 64-byte key used to encrypt the file, or `nil` if it is unencrypted.
  437. @param error If an error occurs, upon return contains an `NSError` object
  438. that describes the problem. If you are not interested in
  439. possible errors, pass in `NULL`.
  440. @return The version of the Realm at `fileURL`, or `RLMNotVersioned` if the version cannot be read.
  441. */
  442. + (uint64_t)schemaVersionAtURL:(NSURL *)fileURL encryptionKey:(nullable NSData *)key error:(NSError **)error
  443. NS_REFINED_FOR_SWIFT;
  444. /**
  445. Performs the given Realm configuration's migration block on a Realm at the given path.
  446. This method is called automatically when opening a Realm for the first time and does
  447. not need to be called explicitly. You can choose to call this method to control
  448. exactly when and how migrations are performed.
  449. @param configuration The Realm configuration used to open and migrate the Realm.
  450. @return The error that occurred while applying the migration, if any.
  451. @see RLMMigration
  452. */
  453. + (BOOL)performMigrationForConfiguration:(RLMRealmConfiguration *)configuration error:(NSError **)error;
  454. #pragma mark - Privileges
  455. /**
  456. Returns the computed privileges which the current user has for this Realm.
  457. This combines all privileges granted on the Realm by all Roles which the
  458. current User is a member of into the final privileges which will be enforced by
  459. the server.
  460. The privilege calculation is done locally using cached data, and inherently may
  461. be stale. It is possible that this method may indicate that an operation is
  462. permitted but the server will still reject it if permission is revoked before
  463. the changes have been integrated on the server.
  464. Non-synchronized Realms always have permission to perform all operations.
  465. @warning This currently returns incorrect results for non-partially-synchronized read-only Realms.
  466. @return The privileges which the current user has for the current Realm.
  467. */
  468. - (struct RLMRealmPrivileges)privilegesForRealm;
  469. /**
  470. Returns the computed privileges which the current user has for the given object.
  471. This combines all privileges granted on the object by all Roles which the
  472. current User is a member of into the final privileges which will be enforced by
  473. the server.
  474. The privilege calculation is done locally using cached data, and inherently may
  475. be stale. It is possible that this method may indicate that an operation is
  476. permitted but the server will still reject it if permission is revoked before
  477. the changes have been integrated on the server.
  478. Non-synchronized Realms always have permission to perform all operations.
  479. The object must be a valid object managed by this Realm. Passing in an
  480. invalidated object, an unmanaged object, or an object managed by a different
  481. Realm will throw an exception.
  482. @warning This currently returns incorrect results for non-partially-synchronized read-only Realms.
  483. @return The privileges which the current user has for the given object.
  484. */
  485. - (struct RLMObjectPrivileges)privilegesForObject:(RLMObject *)object;
  486. /**
  487. Returns the computed privileges which the current user has for the given class.
  488. This combines all privileges granted on the class by all Roles which the
  489. current User is a member of into the final privileges which will be enforced by
  490. the server.
  491. The privilege calculation is done locally using cached data, and inherently may
  492. be stale. It is possible that this method may indicate that an operation is
  493. permitted but the server will still reject it if permission is revoked before
  494. the changes have been integrated on the server.
  495. Non-synchronized Realms always have permission to perform all operations.
  496. @warning This currently returns incorrect results for non-partially-synchronized read-only Realms.
  497. @return The privileges which the current user has for the given object.
  498. */
  499. - (struct RLMClassPrivileges)privilegesForClass:(Class)cls;
  500. /**
  501. Returns the computed privileges which the current user has for the named class.
  502. This combines all privileges granted on the class by all Roles which the
  503. current User is a member of into the final privileges which will be enforced by
  504. the server.
  505. The privilege calculation is done locally using cached data, and inherently may
  506. be stale. It is possible that this method may indicate that an operation is
  507. permitted but the server will still reject it if permission is revoked before
  508. the changes have been integrated on the server.
  509. Non-synchronized Realms always have permission to perform all operations.
  510. @warning This currently returns incorrect results for non-partially-synchronized read-only Realms.
  511. @return The privileges which the current user has for the given object.
  512. */
  513. - (struct RLMClassPrivileges)privilegesForClassNamed:(NSString *)className;
  514. #pragma mark - Unavailable Methods
  515. /**
  516. RLMRealm instances are cached internally by Realm and cannot be created directly.
  517. Use `+[RLMRealm defaultRealm]`, `+[RLMRealm realmWithConfiguration:error:]` or
  518. `+[RLMRealm realmWithURL]` to obtain a reference to an RLMRealm.
  519. */
  520. - (instancetype)init __attribute__((unavailable("Use +defaultRealm, +realmWithConfiguration: or +realmWithURL:.")));
  521. /**
  522. RLMRealm instances are cached internally by Realm and cannot be created directly.
  523. Use `+[RLMRealm defaultRealm]`, `+[RLMRealm realmWithConfiguration:error:]` or
  524. `+[RLMRealm realmWithURL]` to obtain a reference to an RLMRealm.
  525. */
  526. + (instancetype)new __attribute__((unavailable("Use +defaultRealm, +realmWithConfiguration: or +realmWithURL:.")));
  527. /// :nodoc:
  528. - (void)addOrUpdateObjectsFromArray:(id)array __attribute__((unavailable("Renamed to -addOrUpdateObjects:.")));
  529. @end
  530. // MARK: - RLMNotificationToken
  531. /**
  532. A token which is returned from methods which subscribe to changes to a Realm.
  533. Change subscriptions in Realm return an `RLMNotificationToken` instance,
  534. which can be used to unsubscribe from the changes. You must store a strong
  535. reference to the token for as long as you want to continue to receive notifications.
  536. When you wish to stop, call the `-invalidate` method. Notifications are also stopped if
  537. the token is deallocated.
  538. */
  539. @interface RLMNotificationToken : NSObject
  540. /// Stops notifications for the change subscription that returned this token.
  541. - (void)invalidate;
  542. /// Stops notifications for the change subscription that returned this token.
  543. - (void)stop __attribute__((unavailable("Renamed to -invalidate."))) NS_REFINED_FOR_SWIFT;
  544. @end
  545. NS_ASSUME_NONNULL_END