RLMResults.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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 <Realm/RLMCollection.h>
  19. RLM_HEADER_AUDIT_BEGIN(nullability, sendability)
  20. /// A block type used for APIs which asynchronously return a `Results`.
  21. typedef void(^RLMResultsCompletionBlock)(RLMResults * _Nullable, NSError * _Nullable);
  22. /**
  23. Determines wait for download behavior when subscribing on RLMResults.
  24. @see ``[RLMResults subscribeWithName:waitForSync:onQueue:completion:]``
  25. */
  26. typedef NS_ENUM(NSUInteger, RLMWaitForSyncMode) {
  27. /// `subscribeWithName`'s callback will be invoked once matching objects are downloaded
  28. /// from the server only when the subscription is created the first time. If the
  29. /// subscription already exists, the callback is invoked without waiting for new downloads.
  30. RLMWaitForSyncModeOnCreation,
  31. /// `subscribeWithName`'s callback will wait for downloads before being invoked.
  32. /// The callback can't be invoked in this mode unless an internet connection is established or a timeout is set.
  33. RLMWaitForSyncModeAlways,
  34. /// `subscribeWithName`'s callback is always invoked without waiting for downloads.
  35. RLMWaitForSyncModeNever
  36. } NS_SWIFT_NAME(WaitForSyncMode);
  37. @class RLMObject;
  38. /**
  39. `RLMResults` is an auto-updating container type in Realm returned from object
  40. queries. It represents the results of the query in the form of a collection of objects.
  41. `RLMResults` can be queried using the same predicates as `RLMObject` and `RLMArray`,
  42. and you can chain queries to further filter results.
  43. `RLMResults` always reflect the current state of the Realm on the current thread,
  44. including during write transactions on the current thread. The one exception to
  45. this is when using `for...in` fast enumeration, which will always enumerate
  46. over the objects which matched the query when the enumeration is begun, even if
  47. some of them are deleted or modified to be excluded by the filter during the
  48. enumeration.
  49. `RLMResults` are lazily evaluated the first time they are accessed; they only
  50. run queries when the result of the query is requested. This means that
  51. chaining several temporary `RLMResults` to sort and filter your data does not
  52. perform any extra work processing the intermediate state.
  53. Once the results have been evaluated or a notification block has been added,
  54. the results are eagerly kept up-to-date, with the work done to keep them
  55. up-to-date done on a background thread whenever possible.
  56. `RLMResults` cannot be directly instantiated.
  57. */
  58. @interface RLMResults<RLMObjectType> : NSObject<RLMCollection, NSFastEnumeration>
  59. #pragma mark - Properties
  60. /**
  61. The number of objects in the results collection.
  62. */
  63. @property (nonatomic, readonly, assign) NSUInteger count;
  64. /**
  65. The type of the objects in the results collection.
  66. */
  67. @property (nonatomic, readonly, assign) RLMPropertyType type;
  68. /**
  69. Indicates whether the objects in the collection can be `nil`.
  70. */
  71. @property (nonatomic, readwrite, getter = isOptional) BOOL optional;
  72. /**
  73. The class name of the objects contained in the results collection.
  74. Will be `nil` if `type` is not RLMPropertyTypeObject.
  75. */
  76. @property (nonatomic, readonly, copy, nullable) NSString *objectClassName;
  77. /**
  78. The Realm which manages this results collection.
  79. */
  80. @property (nonatomic, readonly) RLMRealm *realm;
  81. /**
  82. Indicates if the results collection is no longer valid.
  83. The results collection becomes invalid if `invalidate` is called on the containing `realm`.
  84. An invalidated results collection can be accessed, but will always be empty.
  85. */
  86. @property (nonatomic, readonly, getter = isInvalidated) BOOL invalidated;
  87. #pragma mark - Accessing Objects from an RLMResults
  88. /**
  89. Returns the object at the index specified.
  90. @param index The index to look up.
  91. @return An object of the type contained in the results collection.
  92. */
  93. - (RLMObjectType)objectAtIndex:(NSUInteger)index;
  94. /**
  95. Returns an array containing the objects in the results at the indexes specified by a given index set.
  96. `nil` will be returned if the index set contains an index out of the arrays bounds.
  97. @param indexes The indexes in the results to retrieve objects from.
  98. @return The objects at the specified indexes.
  99. */
  100. - (nullable NSArray<RLMObjectType> *)objectsAtIndexes:(NSIndexSet *)indexes;
  101. /**
  102. Returns the first object in the results collection.
  103. Returns `nil` if called on an empty results collection.
  104. @return An object of the type contained in the results collection.
  105. */
  106. - (nullable RLMObjectType)firstObject;
  107. /**
  108. Returns the last object in the results collection.
  109. Returns `nil` if called on an empty results collection.
  110. @return An object of the type contained in the results collection.
  111. */
  112. - (nullable RLMObjectType)lastObject;
  113. #pragma mark - Querying Results
  114. /**
  115. Returns the index of an object in the results collection.
  116. Returns `NSNotFound` if the object is not found in the results collection.
  117. @param object An object (of the same type as returned from the `objectClassName` selector).
  118. */
  119. - (NSUInteger)indexOfObject:(RLMObjectType)object;
  120. /**
  121. Returns the index of the first object in the results collection matching the predicate.
  122. @param predicateFormat A predicate format string, optionally followed by a variable number of arguments.
  123. @return The index of the object, or `NSNotFound` if the object is not found in the results collection.
  124. */
  125. - (NSUInteger)indexOfObjectWhere:(NSString *)predicateFormat, ...;
  126. /// :nodoc:
  127. - (NSUInteger)indexOfObjectWhere:(NSString *)predicateFormat args:(va_list)args;
  128. /**
  129. Returns the index of the first object in the results collection matching the predicate.
  130. @param predicate The predicate with which to filter the objects.
  131. @return The index of the object, or `NSNotFound` if the object is not found in the results collection.
  132. */
  133. - (NSUInteger)indexOfObjectWithPredicate:(NSPredicate *)predicate;
  134. /**
  135. Returns all the objects matching the given predicate in the results collection.
  136. @param predicateFormat A predicate format string, optionally followed by a variable number of arguments.
  137. @return An `RLMResults` of objects that match the given predicate.
  138. */
  139. - (RLMResults<RLMObjectType> *)objectsWhere:(NSString *)predicateFormat, ...;
  140. /// :nodoc:
  141. - (RLMResults<RLMObjectType> *)objectsWhere:(NSString *)predicateFormat args:(va_list)args;
  142. /**
  143. Returns all the objects matching the given predicate in the results collection.
  144. @param predicate The predicate with which to filter the objects.
  145. @return An `RLMResults` of objects that match the given predicate.
  146. */
  147. - (RLMResults<RLMObjectType> *)objectsWithPredicate:(NSPredicate *)predicate;
  148. /**
  149. Returns a sorted `RLMResults` from an existing results collection.
  150. @param keyPath The key path to sort by.
  151. @param ascending The direction to sort in.
  152. @return An `RLMResults` sorted by the specified key path.
  153. */
  154. - (RLMResults<RLMObjectType> *)sortedResultsUsingKeyPath:(NSString *)keyPath ascending:(BOOL)ascending;
  155. /**
  156. Returns a sorted `RLMResults` from an existing results collection.
  157. @param properties An array of `RLMSortDescriptor`s to sort by.
  158. @return An `RLMResults` sorted by the specified properties.
  159. */
  160. - (RLMResults<RLMObjectType> *)sortedResultsUsingDescriptors:(NSArray<RLMSortDescriptor *> *)properties;
  161. /**
  162. Returns a distinct `RLMResults` from an existing results collection.
  163. @param keyPaths The key paths used produce distinct results
  164. @return An `RLMResults` made distinct based on the specified key paths
  165. */
  166. - (RLMResults<RLMObjectType> *)distinctResultsUsingKeyPaths:(NSArray<NSString *> *)keyPaths;
  167. #pragma mark - Notifications
  168. /**
  169. Registers a block to be called each time the results collection changes.
  170. The block will be asynchronously called with the initial results collection,
  171. and then called again after each write transaction which changes either any
  172. of the objects in the results, or which objects are in the results.
  173. The `change` parameter will be `nil` the first time the block is called.
  174. For each call after that, it will contain information about
  175. which rows in the results collection were added, removed or modified. If a
  176. write transaction did not modify any objects in the results collection,
  177. the block is not called at all. See the `RLMCollectionChange` documentation for
  178. information on how the changes are reported and an example of updating a
  179. `UITableView`.
  180. The error parameter is present only for backwards compatibility and will always
  181. be `nil`.
  182. At the time when the block is called, the `RLMResults` object will be fully
  183. evaluated and up-to-date, and as long as you do not perform a write transaction
  184. on the same thread or explicitly call `-[RLMRealm refresh]`, accessing it will
  185. never perform blocking work.
  186. Notifications are delivered via the standard run loop, and so can't be
  187. delivered while the run loop is blocked by other activity. When
  188. notifications can't be delivered instantly, multiple notifications may be
  189. coalesced into a single notification. This can include the notification
  190. with the initial results. For example, the following code performs a write
  191. transaction immediately after adding the notification block, so there is no
  192. opportunity for the initial notification to be delivered first. As a
  193. result, the initial notification will reflect the state of the Realm after
  194. the write transaction.
  195. RLMResults<Dog *> *results = [Dog allObjects];
  196. NSLog(@"dogs.count: %zu", dogs.count); // => 0
  197. self.token = [results addNotificationBlock:^(RLMResults *dogs,
  198. RLMCollectionChange *changes,
  199. NSError *error) {
  200. // Only fired once for the example
  201. NSLog(@"dogs.count: %zu", dogs.count); // => 1
  202. }];
  203. [realm transactionWithBlock:^{
  204. Dog *dog = [[Dog alloc] init];
  205. dog.name = @"Rex";
  206. [realm addObject:dog];
  207. }];
  208. // end of run loop execution context
  209. You must retain the returned token for as long as you want updates to continue
  210. to be sent to the block. To stop receiving updates, call `-invalidate` on the token.
  211. @warning This method cannot be called during a write transaction, or when the
  212. containing Realm is read-only.
  213. @param block The block to be called whenever a change occurs.
  214. @return A token which must be held for as long as you want updates to be delivered.
  215. */
  216. - (RLMNotificationToken *)addNotificationBlock:(void (^)(RLMResults<RLMObjectType> *_Nullable results,
  217. RLMCollectionChange *_Nullable change,
  218. NSError *_Nullable error))block
  219. __attribute__((warn_unused_result));
  220. /**
  221. Registers a block to be called each time the results collection changes.
  222. The block will be asynchronously called with the initial results collection,
  223. and then called again after each write transaction which changes either any
  224. of the objects in the results, or which objects are in the results.
  225. The `change` parameter will be `nil` the first time the block is called.
  226. For each call after that, it will contain information about
  227. which rows in the results collection were added, removed or modified. If a
  228. write transaction did not modify any objects in the results collection,
  229. the block is not called at all. See the `RLMCollectionChange` documentation for
  230. information on how the changes are reported and an example of updating a
  231. `UITableView`.
  232. The error parameter is present only for backwards compatibility and will always
  233. be `nil`.
  234. At the time when the block is called, the `RLMResults` object will be fully
  235. evaluated and up-to-date, and as long as you do not perform a write transaction
  236. on the same thread or explicitly call `-[RLMRealm refresh]`, accessing it will
  237. never perform blocking work.
  238. Notifications are delivered on the given queue. If the queue is blocked and
  239. notifications can't be delivered instantly, multiple notifications may be
  240. coalesced into a single notification.
  241. You must retain the returned token for as long as you want updates to continue
  242. to be sent to the block. To stop receiving updates, call `-invalidate` on the token.
  243. @warning This method cannot be called when the containing Realm is read-only or frozen.
  244. @warning The queue must be a serial queue.
  245. @param block The block to be called whenever a change occurs.
  246. @param queue The serial queue to deliver notifications to.
  247. @return A token which must be held for as long as you want updates to be delivered.
  248. */
  249. - (RLMNotificationToken *)addNotificationBlock:(void (^)(RLMResults<RLMObjectType> *_Nullable results,
  250. RLMCollectionChange *_Nullable change,
  251. NSError *_Nullable error))block
  252. queue:(nullable dispatch_queue_t)queue
  253. __attribute__((warn_unused_result));
  254. /**
  255. Registers a block to be called each time the results collection changes.
  256. The block will be asynchronously called with the initial results collection,
  257. and then called again after each write transaction which changes either any
  258. of the objects in the results, or which objects are in the results.
  259. The `change` parameter will be `nil` the first time the block is called.
  260. For each call after that, it will contain information about
  261. which rows in the results collection were added, removed or modified. If a
  262. write transaction did not modify any objects in the results collection,
  263. the block is not called at all. See the `RLMCollectionChange` documentation for
  264. information on how the changes are reported and an example of updating a
  265. `UITableView`.
  266. The error parameter is present only for backwards compatibility and will always
  267. be `nil`.
  268. At the time when the block is called, the `RLMResults` object will be fully
  269. evaluated and up-to-date, and as long as you do not perform a write transaction
  270. on the same thread or explicitly call `-[RLMRealm refresh]`, accessing it will
  271. never perform blocking work.
  272. Notifications are delivered on the given queue. If the queue is blocked and
  273. notifications can't be delivered instantly, multiple notifications may be
  274. coalesced into a single notification.
  275. You must retain the returned token for as long as you want updates to continue
  276. to be sent to the block. To stop receiving updates, call `-invalidate` on the token.
  277. @warning This method cannot be called when the containing Realm is read-only or frozen.
  278. @warning The queue must be a serial queue.
  279. @param block The block to be called whenever a change occurs.
  280. @param queue The serial queue to deliver notifications to.
  281. @param keyPaths The block will be called for changes occurring on these keypaths. If no
  282. key paths are given, notifications are delivered for every property key path.
  283. @return A token which must be held for as long as you want updates to be delivered.
  284. */
  285. - (RLMNotificationToken *)addNotificationBlock:(void (^)(RLMResults<RLMObjectType> *_Nullable results,
  286. RLMCollectionChange *_Nullable change,
  287. NSError *_Nullable error))block
  288. keyPaths:(nullable NSArray<NSString *> *)keyPaths
  289. queue:(nullable dispatch_queue_t)queue
  290. __attribute__((warn_unused_result));
  291. /**
  292. Registers a block to be called each time the results collection changes.
  293. The block will be asynchronously called with the initial results collection,
  294. and then called again after each write transaction which changes either any
  295. of the objects in the results, or which objects are in the results.
  296. The `change` parameter will be `nil` the first time the block is called.
  297. For each call after that, it will contain information about
  298. which rows in the results collection were added, removed or modified. If a
  299. write transaction did not modify any objects in the results collection,
  300. the block is not called at all. See the `RLMCollectionChange` documentation for
  301. information on how the changes are reported and an example of updating a
  302. `UITableView`.
  303. The error parameter is present only for backwards compatibility and will always
  304. be `nil`.
  305. At the time when the block is called, the `RLMResults` object will be fully
  306. evaluated and up-to-date, and as long as you do not perform a write transaction
  307. on the same thread or explicitly call `-[RLMRealm refresh]`, accessing it will
  308. never perform blocking work.
  309. Notifications are delivered via the standard run loop, and so can't be
  310. delivered while the run loop is blocked by other activity. When
  311. notifications can't be delivered instantly, multiple notifications may be
  312. coalesced into a single notification. This can include the notification
  313. with the initial results. For example, the following code performs a write
  314. transaction immediately after adding the notification block, so there is no
  315. opportunity for the initial notification to be delivered first. As a
  316. result, the initial notification will reflect the state of the Realm after
  317. the write transaction.
  318. You must retain the returned token for as long as you want updates to continue
  319. to be sent to the block. To stop receiving updates, call `-invalidate` on the token.
  320. @warning This method cannot be called when the containing Realm is read-only or frozen.
  321. @warning The queue must be a serial queue.
  322. @param block The block to be called whenever a change occurs.
  323. @param keyPaths The block will be called for changes occurring on these keypaths. If no
  324. key paths are given, notifications are delivered for every property key path.
  325. @return A token which must be held for as long as you want updates to be delivered.
  326. */
  327. - (RLMNotificationToken *)addNotificationBlock:(void (^)(RLMResults<RLMObjectType> *_Nullable results,
  328. RLMCollectionChange *_Nullable change,
  329. NSError *_Nullable error))block
  330. keyPaths:(nullable NSArray<NSString *> *)keyPaths
  331. __attribute__((warn_unused_result));
  332. #pragma mark - Flexible Sync
  333. /**
  334. Creates a RLMSyncSubscription matching the RLMResults's local filter.
  335. After committing the subscription to the realm's local subscription set, the method
  336. will wait for downloads according to ``RLMWaitForSyncMode`` behavior.
  337. ### Unnamed subscriptions ###
  338. If `subscribeWithCompletion:` is called without a name whose query matches an unnamed subscription, another subscription is not created.
  339. If `subscribeWithCompletion:` is called without a name whose query matches a named subscription, an additional unnamed subscription is created.
  340. ### Named Subscriptions ###
  341. If `subscribeWithCompletion:` is called with a name whose query matches an unnamed subscription, an additional named subscription is created.
  342. ### Existing name and query ###
  343. If `subscribeWithCompletion:` is called with a name whose name is taken on a different query, the old subscription is updated with the new query.
  344. @note This method opens an update block transaction that creates or updates a subscription.
  345. It's advised to *not* loop over this method in order to create multiple subscriptions at once.
  346. This could create a performance bottleneck by opening multiple unnecessary write transactions.
  347. @see: `[RLMSyncSubscription update:queue:onComplete:]` in order to create multiple subscriptions.
  348. @param queue The queue where the completion dispatches.
  349. @param completion The completion block called after the subscription completes. The callback
  350. will wait for downloads according to the value in `waitForSyncMode`.
  351. @see ``RLMWaitForSyncMode``
  352. @warning This API is currently in `Preview` and may be subject to changes in the future.
  353. */
  354. - (void)subscribeWithCompletionOnQueue:(dispatch_queue_t _Nullable)queue
  355. completion:(RLMResultsCompletionBlock)completion;
  356. /**
  357. Creates a RLMSyncSubscription matching the RLMResults's local filter.
  358. After committing the subscription to the realm's local subscription set, the method
  359. will wait for downloads according to ``RLMWaitForSyncMode`` behavior.
  360. ### Unnamed subscriptions ###
  361. If `subscribeWithCompletion:` is called without a name whose query matches an unnamed subscription, another subscription is not created.
  362. If `subscribeWithCompletion:` is called without a name whose query matches a named subscription, an additional unnamed subscription is created.
  363. ### Named Subscriptions ###
  364. If `subscribeWithCompletion:` is called with a name whose query matches an unnamed subscription, an additional named subscription is created.
  365. ### Existing name and query ###
  366. If `subscribeWithCompletion:` is called with a name whose name is taken on a different query, the old subscription is updated with the new query.
  367. @note This method opens an update block transaction that creates or updates a subscription.
  368. It's advised to *not* loop over this method in order to create multiple subscriptions at once.
  369. This could create a performance bottleneck by opening multiple unnecessary write transactions.
  370. @see: `[RLMSyncSubscription update:queue:onComplete:]` in order to create multiple subscriptions.
  371. @param name The name used to identify the subscription.
  372. @param queue The queue where the completion dispatches.
  373. @param completion The completion block called after the subscription completes. The callback
  374. will wait for downloads according to the value in `waitForSyncMode`.
  375. @see ``RLMWaitForSyncMode``
  376. @warning This API is currently in `Preview` and may be subject to changes in the future.
  377. */
  378. - (void)subscribeWithName:(NSString *_Nullable)name
  379. onQueue:(dispatch_queue_t _Nullable)queue
  380. completion:(RLMResultsCompletionBlock)completion;
  381. /**
  382. Creates a RLMSyncSubscription matching the RLMResults's local filter.
  383. After committing the subscription to the realm's local subscription set, the method
  384. will wait for downloads according to the ``RLMWaitForSyncMode``.
  385. ### Unnamed subscriptions ###
  386. If `subscribeWithCompletion:` is called without a name whose query matches an unnamed subscription, another subscription is not created.
  387. If `subscribeWithCompletion:` is called without a name whose query matches a named subscription, an additional unnamed subscription is created.
  388. ### Named Subscriptions ###
  389. If `subscribeWithCompletion:` is called with a name whose query matches an unnamed subscription, an additional named subscription is created.
  390. ### Existing name and query ###
  391. If `subscribeWithCompletion:` is called with a name whose name is taken on a different query, the old subscription is updated with the new query.
  392. @note This method opens an update block transaction that creates or updates a subscription.
  393. It's advised to *not* loop over this method in order to create multiple subscriptions at once.
  394. This could create a performance bottleneck by opening multiple unnecessary write transactions.
  395. @see: `[RLMSyncSubscription update:queue:onComplete:]` in order to create multiple subscriptions.
  396. @param name The name used to identify the subscription.
  397. @param waitForSyncMode Dictates when the completion handler is called
  398. @param queue The queue where the completion dispatches.
  399. @param completion The completion block called after the subscription completes. The callback
  400. will wait for downloads according to the value in `waitForSyncMode`.
  401. @see ``RLMWaitForSyncMode``
  402. @warning This API is currently in `Preview` and may be subject to changes in the future.
  403. */
  404. - (void)subscribeWithName:(NSString *_Nullable)name
  405. waitForSync:(RLMWaitForSyncMode)waitForSyncMode
  406. onQueue:(dispatch_queue_t _Nullable)queue
  407. completion:(RLMResultsCompletionBlock)completion
  408. __attribute__((swift_attr("@_unsafeInheritExecutor")));
  409. /**
  410. Creates a RLMSyncSubscription matching the RLMResults's local filter.
  411. After committing the subscription to the realm's local subscription set, the method
  412. will wait for downloads according to the ``RLMWaitForSyncMode``.
  413. ### Unnamed subscriptions ###
  414. If `subscribeWithCompletion:` is called without a name whose query matches an unnamed subscription, another subscription is not created.
  415. If `subscribeWithCompletion:` is called without a name whose query matches a named subscription, an additional unnamed subscription is created.
  416. ### Named Subscriptions ###
  417. If `subscribeWithCompletion:` is called with a name whose query matches an unnamed subscription, an additional named subscription is created.
  418. ### Existing name and query ###
  419. If `subscribeWithCompletion:` is called with a name whose name is taken on a different query, the old subscription is updated with the new query.
  420. @note This method opens an update block transaction that creates or updates a subscription.
  421. It's advised to *not* loop over this method in order to create multiple subscriptions at once.
  422. This could create a performance bottleneck by opening multiple unnecessary write transactions.
  423. @see: `[RLMSyncSubscription update:queue:onComplete:]` in order to create multiple subscriptions.
  424. @param name The name used to identify the subscription.
  425. @param waitForSyncMode Dictates when the completion handler is called
  426. @param queue The queue where the completion dispatches.
  427. @param timeout A timeout which ends waiting for downloads
  428. via the completion handler. If the timeout is exceeded the completion
  429. handler returns an error.
  430. @param completion The completion block called after the subscription completes. The callback
  431. will wait for downloads according to the value in `waitForSyncMode`.
  432. @see ``RLMWaitForSyncMode``
  433. @warning This API is currently in `Preview` and may be subject to changes in the future.
  434. */
  435. - (void)subscribeWithName:(NSString *_Nullable)name
  436. waitForSync:(RLMWaitForSyncMode)waitForSyncMode
  437. onQueue:(dispatch_queue_t _Nullable)queue
  438. timeout:(NSTimeInterval)timeout
  439. completion:(RLMResultsCompletionBlock)completion
  440. __attribute__((swift_attr("@_unsafeInheritExecutor")));
  441. /**
  442. Removes a RLMSubscription matching the RLMResults'slocal filter.
  443. The method returns after committing the subscription removal to the
  444. realm's local subscription set. Calling this method will not wait for objects to
  445. be removed from the realm.
  446. Calling unsubscribe on a RLMResults does not remove the local filter from the RLMResults.
  447. After calling unsubscribe, RLMResults may still contain objects because
  448. other subscriptions may exist in the RLMRealm's subscription set.
  449. @note In order for a named subscription to be removed, the RLMResults
  450. must have previously created the subscription.
  451. The `RLMResults` returned in the completion block when calling `subscribe` can be used to unsubscribe from the same subscription.
  452. @note This method opens an update block transaction that creates or updates a subscription.
  453. It's advised to *not* loop over this method in order to create multiple subscriptions at once.
  454. This could create a performance bottleneck by opening multiple unnecessary write transactions.
  455. @see: ``[RLMSyncSubscription update:queue:onComplete:]`` in order to create multiple subscriptions.
  456. @warning This API is currently in `Preview` and may be subject to changes in the future.
  457. */
  458. - (void)unsubscribe;
  459. #pragma mark - Aggregating Property Values
  460. /**
  461. Returns the minimum (lowest) value of the given property among all the objects
  462. represented by the results collection.
  463. NSNumber *min = [results minOfProperty:@"age"];
  464. @warning You cannot use this method on `RLMObject`, `RLMArray`, and `NSData` properties.
  465. @param property The property whose minimum value is desired. Only properties of types `int`, `float`, `double`, and
  466. `NSDate` are supported.
  467. @return The minimum value of the property, or `nil` if the Results are empty.
  468. */
  469. - (nullable id)minOfProperty:(NSString *)property;
  470. /**
  471. Returns the maximum (highest) value of the given property among all the objects represented by the results collection.
  472. NSNumber *max = [results maxOfProperty:@"age"];
  473. @warning You cannot use this method on `RLMObject`, `RLMArray`, and `NSData` properties.
  474. @param property The property whose maximum value is desired. Only properties of
  475. types `int`, `float`, `double`, and `NSDate` are supported.
  476. @return The maximum value of the property, or `nil` if the Results are empty.
  477. */
  478. - (nullable id)maxOfProperty:(NSString *)property;
  479. /**
  480. Returns the sum of the values of a given property over all the objects represented by the results collection.
  481. NSNumber *sum = [results sumOfProperty:@"age"];
  482. @warning You cannot use this method on `RLMObject`, `RLMArray`, and `NSData` properties.
  483. @param property The property whose values should be summed. Only properties of
  484. types `int`, `float`, and `double` are supported.
  485. @return The sum of the given property.
  486. */
  487. - (NSNumber *)sumOfProperty:(NSString *)property;
  488. /**
  489. Returns the average value of a given property over the objects represented by the results collection.
  490. NSNumber *average = [results averageOfProperty:@"age"];
  491. @warning You cannot use this method on `RLMObject`, `RLMArray`, and `NSData` properties.
  492. @param property The property whose average value should be calculated. Only
  493. properties of types `int`, `float`, and `double` are supported.
  494. @return The average value of the given property, or `nil` if the Results are empty.
  495. */
  496. - (nullable NSNumber *)averageOfProperty:(NSString *)property;
  497. /// :nodoc:
  498. - (RLMObjectType)objectAtIndexedSubscript:(NSUInteger)index;
  499. #pragma mark - Sectioned Results
  500. /**
  501. Sorts and sections this collection from a given property key path, returning the result
  502. as an instance of `RLMSectionedResults`.
  503. @param keyPath The property key path to sort on.
  504. @param ascending The direction to sort in.
  505. @param keyBlock A callback which is invoked on each element in the Results collection.
  506. This callback is to return the section key for the element in the collection.
  507. @return An instance of RLMSectionedResults.
  508. */
  509. - (RLMSectionedResults *)sectionedResultsSortedUsingKeyPath:(NSString *)keyPath
  510. ascending:(BOOL)ascending
  511. keyBlock:(RLMSectionedResultsKeyBlock)keyBlock;
  512. /**
  513. Sorts and sections this collection from a given array of sort descriptors, returning the result
  514. as an instance of `RLMSectionedResults`.
  515. @param sortDescriptors An array of `RLMSortDescriptor`s to sort by.
  516. @param keyBlock A callback which is invoked on each element in the Results collection.
  517. This callback is to return the section key for the element in the collection.
  518. @note The primary sort descriptor must be responsible for determining the section key.
  519. @return An instance of RLMSectionedResults.
  520. */
  521. - (RLMSectionedResults *)sectionedResultsUsingSortDescriptors:(NSArray<RLMSortDescriptor *> *)sortDescriptors
  522. keyBlock:(RLMSectionedResultsKeyBlock)keyBlock;
  523. #pragma mark - Freeze
  524. /**
  525. Indicates if the result are frozen.
  526. Frozen Results are immutable and can be accessed from any thread.The objects
  527. read from a frozen Results will also be frozen.
  528. */
  529. @property (nonatomic, readonly, getter=isFrozen) BOOL frozen;
  530. /**
  531. Returns a frozen (immutable) snapshot of these results.
  532. The frozen copy is an immutable collection which contains the same data as
  533. this collection currently contains, but will not update when writes are made
  534. to the containing Realm. Unlike live Results, frozen Results can be accessed
  535. from any thread.
  536. @warning This method cannot be called during a write transaction, or when the
  537. containing Realm is read-only.
  538. @warning Holding onto a frozen collection for an extended period while
  539. performing write transaction on the Realm may result in the Realm
  540. file growing to large sizes. See
  541. `RLMRealmConfiguration.maximumNumberOfActiveVersions` for more
  542. information.
  543. */
  544. - (instancetype)freeze;
  545. /**
  546. Returns a live version of this frozen collection.
  547. This method resolves a reference to a live copy of the same frozen collection.
  548. If called on a live collection, will return itself.
  549. */
  550. - (instancetype)thaw;
  551. #pragma mark - Unavailable Methods
  552. /**
  553. `-[RLMResults init]` is not available because `RLMResults` cannot be created directly.
  554. `RLMResults` can be obtained by querying a Realm.
  555. */
  556. - (instancetype)init __attribute__((unavailable("RLMResults cannot be created directly")));
  557. /**
  558. `+[RLMResults new]` is not available because `RLMResults` cannot be created directly.
  559. `RLMResults` can be obtained by querying a Realm.
  560. */
  561. + (instancetype)new __attribute__((unavailable("RLMResults cannot be created directly")));
  562. @end
  563. /**
  564. `RLMLinkingObjects` is an auto-updating container type. It represents a collection of objects that link to its
  565. parent object.
  566. For more information, please see the "Inverse Relationships" section in the
  567. [documentation](https://www.mongodb.com/docs/realm/sdk/swift/fundamentals/relationships/#relationships).
  568. */
  569. @interface RLMLinkingObjects<RLMObjectType: RLMObject *> : RLMResults
  570. @end
  571. RLM_HEADER_AUDIT_END(nullability, sendability)