RLMSyncSubscription.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2021 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/RLMConstants.h>
  19. @class RLMObjectId;
  20. #pragma mark - Subscription States
  21. /// The current state of the subscription. This can be used for ensuring that
  22. /// the subscriptions are not errored and that it has been successfully
  23. /// synced to the server.
  24. typedef NS_ENUM(NSUInteger, RLMSyncSubscriptionState) {
  25. /// The subscription is complete and the server has sent all the data that matched the subscription
  26. /// queries at the time the subscription set was updated. The server is now in a steady-state
  27. /// synchronization mode where it will stream update as they come.
  28. RLMSyncSubscriptionStateComplete,
  29. /// The subscription encountered an error and synchronization is paused for this Realm. You can
  30. /// find the error calling error in the subscription set to get a description of the error. You can
  31. /// still use the current subscription set to write a subscription.
  32. RLMSyncSubscriptionStateError,
  33. /// The subscription is persisted locally but not yet processed by the server, which means
  34. /// the server hasn't yet returned all the data that matched the updated subscription queries.
  35. RLMSyncSubscriptionStatePending,
  36. /// The subscription set has been super-ceded by an updated one, this typically means that
  37. /// someone is trying to write a subscription on a different instance of the subscription set.
  38. /// You should not use a superseded subscription set and instead obtain a new instance of
  39. /// the subscription set to write a subscription.
  40. RLMSyncSubscriptionStateSuperseded
  41. };
  42. RLM_HEADER_AUDIT_BEGIN(nullability, sendability)
  43. /**
  44. `RLMSyncSubscription` is used to define a Flexible Sync subscription obtained from querying a
  45. subscription set, which can be used to read or remove/update a committed subscription.
  46. */
  47. @interface RLMSyncSubscription : NSObject
  48. /// Name of the subscription. If not specified it will return nil.
  49. @property (nonatomic, readonly, nullable) NSString *name;
  50. /// When the subscription was created. Recorded automatically.
  51. @property (nonatomic, readonly) NSDate *createdAt;
  52. /// When the subscription was last updated. Recorded automatically.
  53. @property (nonatomic, readonly) NSDate *updatedAt;
  54. /**
  55. Updates a Flexible Sync's subscription query with an allowed query which will be used to bootstrap data
  56. from the server when committed.
  57. @warning This method may only be called during a write subscription block.
  58. @param predicateFormat A predicate format string, optionally followed by a variable number of arguments.
  59. */
  60. - (void)updateSubscriptionWhere:(NSString *)predicateFormat, ...;
  61. /// :nodoc:
  62. - (void)updateSubscriptionWhere:(NSString *)predicateFormat
  63. args:(va_list)args;
  64. /**
  65. Updates a Flexible Sync's subscription query with an allowed query which will be used to bootstrap data
  66. from the server when committed.
  67. @warning This method may only be called during a write subscription block.
  68. @param predicate The predicate with which to filter the objects on the server.
  69. */
  70. - (void)updateSubscriptionWithPredicate:(NSPredicate *)predicate;
  71. @end
  72. /**
  73. `RLMSyncSubscriptionSet` is a collection of `RLMSyncSubscription`s. This is the entry point
  74. for adding and removing `RLMSyncSubscription`s.
  75. */
  76. @interface RLMSyncSubscriptionSet : NSObject <NSFastEnumeration>
  77. /// The number of subscriptions in the subscription set.
  78. @property (readonly) NSUInteger count;
  79. /// Gets the error associated to the subscription set. This will be non-nil in case the current
  80. /// state of the subscription set is `RLMSyncSubscriptionStateError`.
  81. @property (nonatomic, readonly, nullable) NSError *error;
  82. /// Gets the state associated to the subscription set.
  83. @property (nonatomic, readonly) RLMSyncSubscriptionState state;
  84. #pragma mark - Batch Update subscriptions
  85. /**
  86. Synchronously performs any transactions (add/remove/update) to the subscription set within the block,
  87. this will not wait for the server to acknowledge and see all the data associated with this collection of subscriptions,
  88. and will return after committing the subscription transactions.
  89. @param block The block containing actions to perform to the subscription set.
  90. */
  91. - (void)update:(__attribute__((noescape)) void(^)(void))block;
  92. /// :nodoc:
  93. - (void)write:(__attribute__((noescape)) void(^)(void))block __attribute__((unavailable("Renamed to -update")));
  94. /**
  95. Synchronously performs any transactions (add/remove/update) to the subscription set within the block. The `onComplete` block is executed after waiting for associated data to be downloaded from the server.
  96. @param block The block containing actions to perform to the subscription set.
  97. @param onComplete A block which is called upon synchronization of
  98. data from the server. The block will be passed `nil`
  99. if the update succeeded, and an error describing the problem
  100. otherwise.
  101. */
  102. - (void)update:(__attribute__((noescape)) void(^)(void))block
  103. onComplete:(nullable void(^RLM_SWIFT_SENDABLE)(NSError *_Nullable))onComplete
  104. __attribute__((swift_async(not_swift_private, 2)))
  105. __attribute__((swift_attr("@_unsafeInheritExecutor")));
  106. /// :nodoc:
  107. - (void)write:(__attribute__((noescape)) void(^)(void))block
  108. onComplete:(void(^)(NSError * _Nullable))onComplete __attribute__((unavailable("Renamed to -update:onComplete.")));
  109. /**
  110. Synchronously performs any transactions (add/remove/update) to the subscription set within the block. The `onComplete` block is executed after waiting for associated data to be downloaded from the server.
  111. @param block The block containing actions to perform to the subscription set.
  112. @param queue The serial queue to deliver notifications to.
  113. @param onComplete A block which is called upon synchronization of
  114. data from the server. The block will be passed `nil`
  115. if the update succeeded, and an error describing the problem
  116. otherwise.
  117. */
  118. - (void)update:(__attribute__((noescape)) void(^)(void))block
  119. queue:(nullable dispatch_queue_t)queue
  120. onComplete:(void(^)(NSError *))onComplete
  121. __attribute__((swift_attr("@_unsafeInheritExecutor")));
  122. #pragma mark - Find subscription
  123. /**
  124. Finds a subscription by the specified name.
  125. @param name The name used to identify the subscription.
  126. @return A subscription for the given name.
  127. */
  128. - (nullable RLMSyncSubscription *)subscriptionWithName:(NSString *)name;
  129. /**
  130. Finds a subscription by the query for the specified object class name.
  131. @param objectClassName The class name for the model class to be queried.
  132. @param predicateFormat A predicate format string, optionally followed by a variable number of arguments.
  133. @return A subscription for the given query..
  134. */
  135. - (nullable RLMSyncSubscription *)subscriptionWithClassName:(NSString *)objectClassName
  136. where:(NSString *)predicateFormat, ...;
  137. /// :nodoc:
  138. - (nullable RLMSyncSubscription *)subscriptionWithClassName:(NSString *)objectClassName
  139. where:(NSString *)predicateFormat
  140. args:(va_list)args;
  141. /**
  142. Finds a subscription by the query for the specified object class name.
  143. @param objectClassName The class name for the model class to be queried.
  144. @param predicate The predicate used to to filter the objects on the server.
  145. @return A subscription for the given query..
  146. */
  147. - (nullable RLMSyncSubscription *)subscriptionWithClassName:(NSString *)objectClassName
  148. predicate:(NSPredicate *)predicate;
  149. #pragma mark - Add a Subscription
  150. /**
  151. Adds a new subscription to the subscription set which will be sent to the server when
  152. committed at the end of a write subscription block.
  153. @warning This method may only be called during a write subscription block.
  154. @param objectClassName The class name for the model class to be queried.
  155. @param predicateFormat A predicate format string, optionally followed by a variable number of arguments.
  156. */
  157. - (void)addSubscriptionWithClassName:(NSString *)objectClassName
  158. where:(NSString *)predicateFormat, ...;
  159. /// :nodoc:
  160. - (void)addSubscriptionWithClassName:(NSString *)objectClassName
  161. where:(NSString *)predicateFormat
  162. args:(va_list)args;
  163. /**
  164. Adds a new subscription to the subscription set which will be sent to the server when
  165. committed at the end of a write subscription block.
  166. @warning This method may only be called during a write subscription block.
  167. @param objectClassName The class name for the model class to be queried.
  168. @param name The name used the identify the subscription.
  169. @param predicateFormat A predicate format string, optionally followed by a variable number of arguments.
  170. */
  171. - (void)addSubscriptionWithClassName:(NSString *)objectClassName
  172. subscriptionName:(NSString *)name
  173. where:(NSString *)predicateFormat, ...;
  174. /// :nodoc:
  175. - (void)addSubscriptionWithClassName:(NSString *)objectClassName
  176. subscriptionName:(NSString *)name
  177. where:(NSString *)predicateFormat
  178. args:(va_list)args;
  179. /**
  180. Adds a new subscription to the subscription set which will be sent to the server when
  181. committed at the end of a write subscription block.
  182. @warning This method may only be called during a write subscription block.
  183. @param objectClassName The class name for the model class to be queried.
  184. @param predicate The predicate defining the query for the subscription.
  185. */
  186. - (void)addSubscriptionWithClassName:(NSString *)objectClassName
  187. predicate:(NSPredicate *)predicate;
  188. /**
  189. Adds a new subscription to the subscription set which will be sent to the server when
  190. committed at the end of a write subscription block.
  191. @warning This method may only be called during a write subscription block.
  192. @param objectClassName The class name for the model class to be queried.
  193. @param name The name used to identify the subscription.
  194. @param predicate The predicate defining the query for the subscription.
  195. */
  196. - (void)addSubscriptionWithClassName:(NSString *)objectClassName
  197. subscriptionName:(nullable NSString *)name
  198. predicate:(NSPredicate *)predicate;
  199. #pragma mark - Remove Subscription
  200. /**
  201. Removes a subscription with the specified name from the subscription set.
  202. @warning This method may only be called during a write subscription block.
  203. @param name The name used the identify the subscription.
  204. */
  205. - (void)removeSubscriptionWithName:(NSString *)name;
  206. /**
  207. Removes a subscription with the specified query for the object class from the subscription set.
  208. @warning This method may only be called during a write subscription block.
  209. @param objectClassName The class name for the model class to be queried.
  210. @param predicateFormat A predicate format string, optionally followed by a variable number of arguments.
  211. */
  212. - (void)removeSubscriptionWithClassName:(NSString *)objectClassName
  213. where:(NSString *)predicateFormat, ...;
  214. /// :nodoc:
  215. - (void)removeSubscriptionWithClassName:(NSString *)objectClassName
  216. where:(NSString *)predicateFormat
  217. args:(va_list)args;
  218. /**
  219. Removes a subscription with the specified query for the object class from the subscription set.
  220. @warning This method may only be called during a write subscription block.
  221. @param objectClassName The class name for the model class to be queried.
  222. @param predicate The predicate which will be used to identify the subscription to be removed.
  223. */
  224. - (void)removeSubscriptionWithClassName:(NSString *)objectClassName
  225. predicate:(NSPredicate *)predicate;
  226. /**
  227. Removes the subscription from the subscription set.
  228. @warning This method may only be called during a write subscription block.
  229. @param subscription An instance of the subscription to be removed.
  230. */
  231. - (void)removeSubscription:(RLMSyncSubscription *)subscription;
  232. #pragma mark - Remove Subscriptions
  233. /**
  234. Removes all subscription from the subscription set.
  235. @warning This method may only be called during a write subscription block.
  236. @warning Removing all subscriptions will result in an error if no new subscription is added. Server should
  237. acknowledge at least one subscription.
  238. */
  239. - (void)removeAllSubscriptions;
  240. /**
  241. Removes all subscriptions without a name from the subscription set.
  242. @warning This method may only be called during a write subscription block.
  243. @warning Removing all subscriptions will result in an error if no new subscription is added. Server should
  244. acknowledge at least one subscription.
  245. */
  246. - (void)removeAllUnnamedSubscriptions;
  247. /**
  248. Removes all subscription with the specified class name.
  249. @param className The class name for the model class to be queried.
  250. @warning This method may only be called during a write subscription block.
  251. */
  252. - (void)removeAllSubscriptionsWithClassName:(NSString *)className;
  253. #pragma mark - SubscriptionSet Collection
  254. /**
  255. Returns the subscription at the given `index`.
  256. @param index The index.
  257. @return A subscription for the given index in the subscription set.
  258. */
  259. - (nullable RLMSyncSubscription *)objectAtIndex:(NSUInteger)index;
  260. /**
  261. Returns the first object in the subscription set list, or `nil` if the subscriptions are empty.
  262. @return A subscription.
  263. */
  264. - (nullable RLMSyncSubscription *)firstObject;
  265. /**
  266. Returns the last object in the subscription set, or `nil` if the subscriptions are empty.
  267. @return A subscription.
  268. */
  269. - (nullable RLMSyncSubscription *)lastObject;
  270. #pragma mark - Subscript
  271. /**
  272. Returns the subscription at the given `index`.
  273. @param index The index.
  274. @return A subscription for the given index in the subscription set.
  275. */
  276. - (id)objectAtIndexedSubscript:(NSUInteger)index;
  277. @end
  278. RLM_HEADER_AUDIT_END(nullability, sendability)