RLMObject.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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 <Realm/RLMObjectBase.h>
  20. #import <Realm/RLMThreadSafeReference.h>
  21. NS_ASSUME_NONNULL_BEGIN
  22. @class RLMNotificationToken;
  23. @class RLMObjectSchema;
  24. @class RLMPropertyChange;
  25. @class RLMPropertyDescriptor;
  26. @class RLMRealm;
  27. @class RLMResults<RLMObjectType>;
  28. /**
  29. `RLMObject` is a base class for model objects representing data stored in Realms.
  30. Define your model classes by subclassing `RLMObject` and adding properties to be managed.
  31. Then instantiate and use your custom subclasses instead of using the `RLMObject` class directly.
  32. // Dog.h
  33. @interface Dog : RLMObject
  34. @property NSString *name;
  35. @property BOOL adopted;
  36. @end
  37. // Dog.m
  38. @implementation Dog
  39. @end //none needed
  40. ### Supported property types
  41. - `NSString`
  42. - `NSInteger`, `int`, `long`, `float`, and `double`
  43. - `BOOL` or `bool`
  44. - `NSDate`
  45. - `NSData`
  46. - `NSNumber<X>`, where `X` is one of `RLMInt`, `RLMFloat`, `RLMDouble` or `RLMBool`, for optional number properties
  47. - `RLMObject` subclasses, to model many-to-one relationships.
  48. - `RLMArray<X>`, where `X` is an `RLMObject` subclass, to model many-to-many relationships.
  49. ### Querying
  50. You can initiate queries directly via the class methods: `allObjects`, `objectsWhere:`, and `objectsWithPredicate:`.
  51. These methods allow you to easily query a custom subclass for instances of that class in the default Realm.
  52. To search in a Realm other than the default Realm, use the `allObjectsInRealm:`, `objectsInRealm:where:`,
  53. and `objectsInRealm:withPredicate:` class methods.
  54. @see `RLMRealm`
  55. ### Relationships
  56. See our [Cocoa guide](https://realm.io/docs/objc/latest#relationships) for more details.
  57. ### Key-Value Observing
  58. All `RLMObject` properties (including properties you create in subclasses) are
  59. [Key-Value Observing compliant](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html),
  60. except for `realm` and `objectSchema`.
  61. Keep the following tips in mind when observing Realm objects:
  62. 1. Unlike `NSMutableArray` properties, `RLMArray` properties do not require
  63. using the proxy object returned from `-mutableArrayValueForKey:`, or defining
  64. KVC mutation methods on the containing class. You can simply call methods on
  65. the `RLMArray` directly; any changes will be automatically observed by the containing
  66. object.
  67. 2. Unmanaged `RLMObject` instances cannot be added to a Realm while they have any
  68. observed properties.
  69. 3. Modifying managed `RLMObject`s within `-observeValueForKeyPath:ofObject:change:context:`
  70. is not recommended. Properties may change even when the Realm is not in a write
  71. transaction (for example, when `-[RLMRealm refresh]` is called after changes
  72. are made on a different thread), and notifications sent prior to the change
  73. being applied (when `NSKeyValueObservingOptionPrior` is used) may be sent at
  74. times when you *cannot* begin a write transaction.
  75. */
  76. @interface RLMObject : RLMObjectBase <RLMThreadConfined>
  77. #pragma mark - Creating & Initializing Objects
  78. /**
  79. Creates an unmanaged instance of a Realm object.
  80. Call `addObject:` on an `RLMRealm` instance to add an unmanaged object into that Realm.
  81. @see `[RLMRealm addObject:]`
  82. */
  83. - (instancetype)init NS_DESIGNATED_INITIALIZER;
  84. /**
  85. Creates an unmanaged instance of a Realm object.
  86. Pass in an `NSArray` or `NSDictionary` instance to set the values of the object's properties.
  87. Call `addObject:` on an `RLMRealm` instance to add an unmanaged object into that Realm.
  88. @see `[RLMRealm addObject:]`
  89. */
  90. - (instancetype)initWithValue:(id)value NS_DESIGNATED_INITIALIZER;
  91. /**
  92. Returns the class name for a Realm object subclass.
  93. @warning Do not override. Realm relies on this method returning the exact class
  94. name.
  95. @return The class name for the model class.
  96. */
  97. + (NSString *)className;
  98. /**
  99. Creates an instance of a Realm object with a given value, and adds it to the default Realm.
  100. If nested objects are included in the argument, `createInDefaultRealmWithValue:` will be recursively called
  101. on them.
  102. The `value` argument can be a key-value coding compliant object, an array or dictionary returned from the methods in
  103. `NSJSONSerialization`, or an array containing one element for each managed property.
  104. An exception will be thrown if any required properties are not present and those properties
  105. were not defined with default values.
  106. If the `value` argument is an array, all properties must be present, valid and in the same
  107. order as the properties defined in the model.
  108. @param value The value used to populate the object.
  109. @see `defaultPropertyValues`
  110. */
  111. + (instancetype)createInDefaultRealmWithValue:(id)value;
  112. /**
  113. Creates an instance of a Realm object with a given value, and adds it to the specified Realm.
  114. If nested objects are included in the argument, `createInRealm:withValue:` will be recursively called
  115. on them.
  116. The `value` argument can be a key-value coding compliant object, an array or dictionary returned from the methods in
  117. `NSJSONSerialization`, or an array containing one element for each managed property.
  118. An exception will be thrown if any required properties are not present and those properties
  119. were not defined with default values.
  120. If the `value` argument is an array, all properties must be present, valid and in the same
  121. order as the properties defined in the model.
  122. @param realm The Realm which should manage the newly-created object.
  123. @param value The value used to populate the object.
  124. @see `defaultPropertyValues`
  125. */
  126. + (instancetype)createInRealm:(RLMRealm *)realm withValue:(id)value;
  127. /**
  128. Creates or updates a Realm object within the default Realm.
  129. This method may only be called on Realm object types with a primary key defined. If there is already
  130. an object with the same primary key value in the default Realm, its values are updated and the object
  131. is returned. Otherwise, this method creates and populates a new instance of the object in the default Realm.
  132. If nested objects are included in the argument, `createOrUpdateInDefaultRealmWithValue:` will be
  133. recursively called on them if they have primary keys, `createInDefaultRealmWithValue:` if they do not.
  134. The `value` argument is used to populate the object. It can be a Realm object, a key-value coding
  135. compliant object, an array or dictionary returned from the methods in `NSJSONSerialization`, or an
  136. array containing one element for each managed property.
  137. If the object is being created, an exception will be thrown if any required properties
  138. are not present and those properties were not defined with default values.
  139. If the `value` argument is a Realm object already managed by the default Realm, the
  140. argument's type is the same as the receiver, and the objects have identical values for
  141. their managed properties, this method does nothing.
  142. If the object is being updated, each property defined in its schema will be set by copying from
  143. `value` using key-value coding. If the `value` argument does not respond to `valueForKey:` for a
  144. given property name (or getter name, if defined), that value will remain untouched.
  145. Nullable properties on the object can be set to nil by using `NSNull` as the updated value.
  146. Each property is set even if the existing value is the same as the new value being set, and
  147. notifications will report them all being changed. See `createOrUpdateModifiedInDefaultRealmWithValue:`
  148. for a version of this function which only sets the values which have changed.
  149. If the `value` argument is an array, all properties must be present, valid and in the same
  150. order as the properties defined in the model.
  151. @param value The value used to populate the object.
  152. @see `defaultPropertyValues`, `primaryKey`
  153. */
  154. + (instancetype)createOrUpdateInDefaultRealmWithValue:(id)value;
  155. /**
  156. Creates or updates a Realm object within the default Realm.
  157. This method may only be called on Realm object types with a primary key defined. If there is already
  158. an object with the same primary key value in the default Realm, its values are updated and the object
  159. is returned. Otherwise, this method creates and populates a new instance of the object in the default Realm.
  160. If nested objects are included in the argument, `createOrUpdateModifiedInDefaultRealmWithValue:` will be
  161. recursively called on them if they have primary keys, `createInDefaultRealmWithValue:` if they do not.
  162. The `value` argument is used to populate the object. It can be a Realm object, a key-value coding
  163. compliant object, an array or dictionary returned from the methods in `NSJSONSerialization`, or an
  164. array containing one element for each managed property.
  165. If the object is being created, an exception will be thrown if any required properties
  166. are not present and those properties were not defined with default values.
  167. If the `value` argument is a Realm object already managed by the default Realm, the
  168. argument's type is the same as the receiver, and the objects have identical values for
  169. their managed properties, this method does nothing.
  170. If the object is being updated, each property defined in its schema will be set by copying from
  171. `value` using key-value coding. If the `value` argument does not respond to `valueForKey:` for a
  172. given property name (or getter name, if defined), that value will remain untouched.
  173. Nullable properties on the object can be set to nil by using `NSNull` as the updated value.
  174. Unlike `createOrUpdateInDefaultRealmWithValue:`, only properties which have changed in value are
  175. set, and any change notifications produced by this call will report only which properies have
  176. actually changed.
  177. Checking which properties have changed imposes a small amount of overhead, and so this method
  178. may be slower when all or nearly all of the properties being set have changed. If most or all
  179. of the properties being set have not changed, this method will be much faster than unconditionally
  180. setting all of them, and will also reduce how much data has to be written to the Realm, saving
  181. both i/o time and disk space.
  182. If the `value` argument is an array, all properties must be present, valid and in the same
  183. order as the properties defined in the model.
  184. @param value The value used to populate the object.
  185. @see `defaultPropertyValues`, `primaryKey`
  186. */
  187. + (instancetype)createOrUpdateModifiedInDefaultRealmWithValue:(id)value;
  188. /**
  189. Creates or updates an Realm object within a specified Realm.
  190. This method may only be called on Realm object types with a primary key defined. If there is already
  191. an object with the same primary key value in the given Realm, its values are updated and the object
  192. is returned. Otherwise this method creates and populates a new instance of this object in the given Realm.
  193. If nested objects are included in the argument, `createOrUpdateInRealm:withValue:` will be
  194. recursively called on them if they have primary keys, `createInRealm:withValue:` if they do not.
  195. The `value` argument is used to populate the object. It can be a Realm object, a key-value coding
  196. compliant object, an array or dictionary returned from the methods in `NSJSONSerialization`, or an
  197. array containing one element for each managed property.
  198. If the object is being created, an exception will be thrown if any required properties
  199. are not present and those properties were not defined with default values.
  200. If the `value` argument is a Realm object already managed by the given Realm, the
  201. argument's type is the same as the receiver, and the objects have identical values for
  202. their managed properties, this method does nothing.
  203. If the object is being updated, each property defined in its schema will be set by copying from
  204. `value` using key-value coding. If the `value` argument does not respond to `valueForKey:` for a
  205. given property name (or getter name, if defined), that value will remain untouched.
  206. Nullable properties on the object can be set to nil by using `NSNull` as the updated value.
  207. Each property is set even if the existing value is the same as the new value being set, and
  208. notifications will report them all being changed. See `createOrUpdateModifiedInRealm:withValue:`
  209. for a version of this function which only sets the values which have changed.
  210. If the `value` argument is an array, all properties must be present, valid and in the same
  211. order as the properties defined in the model.
  212. @param realm The Realm which should own the object.
  213. @param value The value used to populate the object.
  214. @see `defaultPropertyValues`, `primaryKey`
  215. */
  216. + (instancetype)createOrUpdateInRealm:(RLMRealm *)realm withValue:(id)value;
  217. /**
  218. Creates or updates an Realm object within a specified Realm.
  219. This method may only be called on Realm object types with a primary key defined. If there is already
  220. an object with the same primary key value in the given Realm, its values are updated and the object
  221. is returned. Otherwise this method creates and populates a new instance of this object in the given Realm.
  222. If nested objects are included in the argument, `createOrUpdateInRealm:withValue:` will be
  223. recursively called on them if they have primary keys, `createInRealm:withValue:` if they do not.
  224. The `value` argument is used to populate the object. It can be a Realm object, a key-value coding
  225. compliant object, an array or dictionary returned from the methods in `NSJSONSerialization`, or an
  226. array containing one element for each managed property.
  227. If the object is being created, an exception will be thrown if any required properties
  228. are not present and those properties were not defined with default values.
  229. If the `value` argument is a Realm object already managed by the given Realm, the
  230. argument's type is the same as the receiver, and the objects have identical values for
  231. their managed properties, this method does nothing.
  232. If the object is being updated, each property defined in its schema will be set by copying from
  233. `value` using key-value coding. If the `value` argument does not respond to `valueForKey:` for a
  234. given property name (or getter name, if defined), that value will remain untouched.
  235. Nullable properties on the object can be set to nil by using `NSNull` as the updated value.
  236. Unlike `createOrUpdateInRealm:withValue:`, only properties which have changed in value are
  237. set, and any change notifications produced by this call will report only which properies have
  238. actually changed.
  239. Checking which properties have changed imposes a small amount of overhead, and so this method
  240. may be slower when all or nearly all of the properties being set have changed. If most or all
  241. of the properties being set have not changed, this method will be much faster than unconditionally
  242. setting all of them, and will also reduce how much data has to be written to the Realm, saving
  243. both i/o time and disk space.
  244. If the `value` argument is an array, all properties must be present, valid and in the same
  245. order as the properties defined in the model.
  246. @param realm The Realm which should own the object.
  247. @param value The value used to populate the object.
  248. @see `defaultPropertyValues`, `primaryKey`
  249. */
  250. + (instancetype)createOrUpdateModifiedInRealm:(RLMRealm *)realm withValue:(id)value;
  251. #pragma mark - Properties
  252. /**
  253. The Realm which manages the object, or `nil` if the object is unmanaged.
  254. */
  255. @property (nonatomic, readonly, nullable) RLMRealm *realm;
  256. /**
  257. The object schema which lists the managed properties for the object.
  258. */
  259. @property (nonatomic, readonly) RLMObjectSchema *objectSchema;
  260. /**
  261. Indicates if the object can no longer be accessed because it is now invalid.
  262. An object can no longer be accessed if the object has been deleted from the Realm that manages it, or
  263. if `invalidate` is called on that Realm.
  264. */
  265. @property (nonatomic, readonly, getter = isInvalidated) BOOL invalidated;
  266. #pragma mark - Customizing your Objects
  267. /**
  268. Returns an array of property names for properties which should be indexed.
  269. Only string, integer, boolean, and `NSDate` properties are supported.
  270. @return An array of property names.
  271. */
  272. + (NSArray<NSString *> *)indexedProperties;
  273. /**
  274. Override this method to specify the default values to be used for each property.
  275. @return A dictionary mapping property names to their default values.
  276. */
  277. + (nullable NSDictionary *)defaultPropertyValues;
  278. /**
  279. Override this method to specify the name of a property to be used as the primary key.
  280. Only properties of types `RLMPropertyTypeString` and `RLMPropertyTypeInt` can be designated as the primary key.
  281. Primary key properties enforce uniqueness for each value whenever the property is set, which incurs minor overhead.
  282. Indexes are created automatically for primary key properties.
  283. @return The name of the property designated as the primary key.
  284. */
  285. + (nullable NSString *)primaryKey;
  286. /**
  287. Override this method to specify the names of properties to ignore. These properties will not be managed by the Realm
  288. that manages the object.
  289. @return An array of property names to ignore.
  290. */
  291. + (nullable NSArray<NSString *> *)ignoredProperties;
  292. /**
  293. Override this method to specify the names of properties that are non-optional (i.e. cannot be assigned a `nil` value).
  294. By default, all properties of a type whose values can be set to `nil` are considered optional properties.
  295. To require that an object in a Realm always store a non-`nil` value for a property,
  296. add the name of the property to the array returned from this method.
  297. Properties of `RLMObject` type cannot be non-optional. Array and `NSNumber` properties
  298. can be non-optional, but there is no reason to do so: arrays do not support storing nil, and
  299. if you want a non-optional number you should instead use the primitive type.
  300. @return An array of property names that are required.
  301. */
  302. + (NSArray<NSString *> *)requiredProperties;
  303. /**
  304. Override this method to provide information related to properties containing linking objects.
  305. Each property of type `RLMLinkingObjects` must have a key in the dictionary returned by this method consisting
  306. of the property name. The corresponding value must be an instance of `RLMPropertyDescriptor` that describes the class
  307. and property that the property is linked to.
  308. return @{ @"owners": [RLMPropertyDescriptor descriptorWithClass:Owner.class propertyName:@"dogs"] };
  309. @return A dictionary mapping property names to `RLMPropertyDescriptor` instances.
  310. */
  311. + (NSDictionary<NSString *, RLMPropertyDescriptor *> *)linkingObjectsProperties;
  312. #pragma mark - Getting & Querying Objects from the Default Realm
  313. /**
  314. Returns all objects of this object type from the default Realm.
  315. @return An `RLMResults` containing all objects of this type in the default Realm.
  316. */
  317. + (RLMResults *)allObjects;
  318. /**
  319. Returns all objects of this object type matching the given predicate from the default Realm.
  320. @param predicateFormat A predicate format string, optionally followed by a variable number of arguments.
  321. @return An `RLMResults` containing all objects of this type in the default Realm that match the given predicate.
  322. */
  323. + (RLMResults *)objectsWhere:(NSString *)predicateFormat, ...;
  324. /// :nodoc:
  325. + (RLMResults<__kindof RLMObject *> *)objectsWhere:(NSString *)predicateFormat args:(va_list)args;
  326. /**
  327. Returns all objects of this object type matching the given predicate from the default Realm.
  328. @param predicate The predicate with which to filter the objects.
  329. @return An `RLMResults` containing all objects of this type in the default Realm that match the given predicate.
  330. */
  331. + (RLMResults *)objectsWithPredicate:(nullable NSPredicate *)predicate;
  332. /**
  333. Retrieves the single instance of this object type with the given primary key from the default Realm.
  334. Returns the object from the default Realm which has the given primary key, or
  335. `nil` if the object does not exist. This is slightly faster than the otherwise
  336. equivalent `[[SubclassName objectsWhere:@"primaryKeyPropertyName = %@", key] firstObject]`.
  337. This method requires that `primaryKey` be overridden on the receiving subclass.
  338. @return An object of this object type, or `nil` if an object with the given primary key does not exist.
  339. @see `-primaryKey`
  340. */
  341. + (nullable instancetype)objectForPrimaryKey:(nullable id)primaryKey NS_SWIFT_NAME(object(forPrimaryKey:));
  342. #pragma mark - Querying Specific Realms
  343. /**
  344. Returns all objects of this object type from the specified Realm.
  345. @param realm The Realm to query.
  346. @return An `RLMResults` containing all objects of this type in the specified Realm.
  347. */
  348. + (RLMResults *)allObjectsInRealm:(RLMRealm *)realm;
  349. /**
  350. Returns all objects of this object type matching the given predicate from the specified Realm.
  351. @param predicateFormat A predicate format string, optionally followed by a variable number of arguments.
  352. @param realm The Realm to query.
  353. @return An `RLMResults` containing all objects of this type in the specified Realm that match the given predicate.
  354. */
  355. + (RLMResults *)objectsInRealm:(RLMRealm *)realm where:(NSString *)predicateFormat, ...;
  356. /// :nodoc:
  357. + (RLMResults<__kindof RLMObject *> *)objectsInRealm:(RLMRealm *)realm where:(NSString *)predicateFormat args:(va_list)args;
  358. /**
  359. Returns all objects of this object type matching the given predicate from the specified Realm.
  360. @param predicate A predicate to use to filter the elements.
  361. @param realm The Realm to query.
  362. @return An `RLMResults` containing all objects of this type in the specified Realm that match the given predicate.
  363. */
  364. + (RLMResults *)objectsInRealm:(RLMRealm *)realm withPredicate:(nullable NSPredicate *)predicate;
  365. /**
  366. Retrieves the single instance of this object type with the given primary key from the specified Realm.
  367. Returns the object from the specified Realm which has the given primary key, or
  368. `nil` if the object does not exist. This is slightly faster than the otherwise
  369. equivalent `[[SubclassName objectsInRealm:realm where:@"primaryKeyPropertyName = %@", key] firstObject]`.
  370. This method requires that `primaryKey` be overridden on the receiving subclass.
  371. @return An object of this object type, or `nil` if an object with the given primary key does not exist.
  372. @see `-primaryKey`
  373. */
  374. + (nullable instancetype)objectInRealm:(RLMRealm *)realm forPrimaryKey:(nullable id)primaryKey NS_SWIFT_NAME(object(in:forPrimaryKey:));
  375. #pragma mark - Notifications
  376. /**
  377. A callback block for `RLMObject` notifications.
  378. If the object is deleted from the managing Realm, the block is called with
  379. `deleted` set to `YES` and the other two arguments are `nil`. The block will
  380. never be called again after this.
  381. If the object is modified, the block will be called with `deleted` set to
  382. `NO`, a `nil` error, and an array of `RLMPropertyChange` objects which
  383. indicate which properties of the objects were modified.
  384. If an error occurs, `deleted` will be `NO`, `changes` will be `nil`, and
  385. `error` will include information about the error. The block will never be
  386. called again after an error occurs.
  387. */
  388. typedef void (^RLMObjectChangeBlock)(BOOL deleted,
  389. NSArray<RLMPropertyChange *> *_Nullable changes,
  390. NSError *_Nullable error);
  391. /**
  392. Registers a block to be called each time the object changes.
  393. The block will be asynchronously called after each write transaction which
  394. deletes the object or modifies any of the managed properties of the object,
  395. including self-assignments that set a property to its existing value.
  396. For write transactions performed on different threads or in differen
  397. processes, the block will be called when the managing Realm is
  398. (auto)refreshed to a version including the changes, while for local write
  399. transactions it will be called at some point in the future after the write
  400. transaction is committed.
  401. Notifications are delivered via the standard run loop, and so can't be
  402. delivered while the run loop is blocked by other activity. When notifications
  403. can't be delivered instantly, multiple notifications may be coalesced into a
  404. single notification.
  405. Unlike with `RLMArray` and `RLMResults`, there is no "initial" callback made
  406. after you add a new notification block.
  407. Only objects which are managed by a Realm can be observed in this way. You
  408. must retain the returned token for as long as you want updates to be sent to
  409. the block. To stop receiving updates, call `-invalidate` on the token.
  410. It is safe to capture a strong reference to the observed object within the
  411. callback block. There is no retain cycle due to that the callback is retained
  412. by the returned token and not by the object itself.
  413. @warning This method cannot be called during a write transaction, when the
  414. containing Realm is read-only, or on an unmanaged object.
  415. @param block The block to be called whenever a change occurs.
  416. @return A token which must be held for as long as you want updates to be delivered.
  417. */
  418. - (RLMNotificationToken *)addNotificationBlock:(RLMObjectChangeBlock)block;
  419. #pragma mark - Other Instance Methods
  420. /**
  421. Returns YES if another Realm object instance points to the same object as the receiver in the Realm managing
  422. the receiver.
  423. For object types with a primary, key, `isEqual:` is overridden to use the same logic as this
  424. method (along with a corresponding implementation for `hash`).
  425. @param object The object to compare the receiver to.
  426. @return Whether the object represents the same object as the receiver.
  427. */
  428. - (BOOL)isEqualToObject:(RLMObject *)object;
  429. #pragma mark - Dynamic Accessors
  430. /// :nodoc:
  431. - (nullable id)objectForKeyedSubscript:(NSString *)key;
  432. /// :nodoc:
  433. - (void)setObject:(nullable id)obj forKeyedSubscript:(NSString *)key;
  434. @end
  435. /**
  436. Information about a specific property which changed in an `RLMObject` change notification.
  437. */
  438. @interface RLMPropertyChange : NSObject
  439. /**
  440. The name of the property which changed.
  441. */
  442. @property (nonatomic, readonly, strong) NSString *name;
  443. /**
  444. The value of the property before the change occurred. This will always be `nil`
  445. if the change happened on the same thread as the notification and for `RLMArray`
  446. properties.
  447. For object properties this will give the object which was previously linked to,
  448. but that object will have its new values and not the values it had before the
  449. changes. This means that `previousValue` may be a deleted object, and you will
  450. need to check `invalidated` before accessing any of its properties.
  451. */
  452. @property (nonatomic, readonly, strong, nullable) id previousValue;
  453. /**
  454. The value of the property after the change occurred. This will always be `nil`
  455. for `RLMArray` properties.
  456. */
  457. @property (nonatomic, readonly, strong, nullable) id value;
  458. @end
  459. #pragma mark - RLMArray Property Declaration
  460. /**
  461. Properties on `RLMObject`s of type `RLMArray` must have an associated type. A type is associated
  462. with an `RLMArray` property by defining a protocol for the object type that the array should contain.
  463. To define the protocol for an object, you can use the macro RLM_ARRAY_TYPE:
  464. RLM_ARRAY_TYPE(ObjectType)
  465. ...
  466. @property RLMArray<ObjectType *><ObjectType> *arrayOfObjectTypes;
  467. */
  468. #define RLM_ARRAY_TYPE(RLM_OBJECT_SUBCLASS)\
  469. @protocol RLM_OBJECT_SUBCLASS <NSObject> \
  470. @end
  471. NS_ASSUME_NONNULL_END