FIRQuery.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #import "FIRListenerRegistration.h"
  18. @class FIRFieldPath;
  19. @class FIRFirestore;
  20. @class FIRQuerySnapshot;
  21. @class FIRDocumentSnapshot;
  22. NS_ASSUME_NONNULL_BEGIN
  23. /**
  24. * Options for use with `[FIRQuery addSnapshotListener]` to control the behavior of the snapshot
  25. * listener.
  26. */
  27. NS_SWIFT_NAME(QueryListenOptions)
  28. @interface FIRQueryListenOptions : NSObject
  29. + (instancetype)options NS_SWIFT_UNAVAILABLE("Use initializer");
  30. - (instancetype)init;
  31. @property(nonatomic, assign, readonly) BOOL includeQueryMetadataChanges;
  32. /**
  33. * Sets the includeQueryMetadataChanges option which controls whether metadata-only changes on the
  34. * query (i.e. only `FIRQuerySnapshot.metadata` changed) should trigger snapshot events. Default is
  35. * NO.
  36. *
  37. * @param includeQueryMetadataChanges Whether to raise events for metadata-only changes on the
  38. * query.
  39. * @return The receiver is returned for optional method chaining.
  40. */
  41. - (instancetype)includeQueryMetadataChanges:(BOOL)includeQueryMetadataChanges
  42. NS_SWIFT_NAME(includeQueryMetadataChanges(_:));
  43. @property(nonatomic, assign, readonly) BOOL includeDocumentMetadataChanges;
  44. /**
  45. * Sets the includeDocumentMetadataChanges option which controls whether document metadata-only
  46. * changes (i.e. only `FIRDocumentSnapshot.metadata` on a document contained in the query
  47. * changed) should trigger snapshot events. Default is NO.
  48. *
  49. * @param includeDocumentMetadataChanges Whether to raise events for document metadata-only changes.
  50. * @return The receiver is returned for optional method chaining.
  51. */
  52. - (instancetype)includeDocumentMetadataChanges:(BOOL)includeDocumentMetadataChanges
  53. NS_SWIFT_NAME(includeDocumentMetadataChanges(_:));
  54. @end
  55. typedef void (^FIRQuerySnapshotBlock)(FIRQuerySnapshot *_Nullable snapshot,
  56. NSError *_Nullable error);
  57. /**
  58. * A `FIRQuery` refers to a Query which you can read or listen to. You can also construct
  59. * refined `FIRQuery` objects by adding filters and ordering.
  60. */
  61. NS_SWIFT_NAME(Query)
  62. @interface FIRQuery : NSObject
  63. /** */
  64. - (id)init __attribute__((unavailable("FIRQuery cannot be created directly.")));
  65. /** The `FIRFirestore` for the Firestore database (useful for performing transactions, etc.). */
  66. @property(nonatomic, strong, readonly) FIRFirestore *firestore;
  67. #pragma mark - Retrieving Data
  68. /**
  69. * Reads the documents matching this query.
  70. *
  71. * @param completion a block to execute once the documents have been successfully read.
  72. * documentSet will be `nil` only if error is `non-nil`.
  73. */
  74. - (void)getDocumentsWithCompletion:(FIRQuerySnapshotBlock)completion
  75. NS_SWIFT_NAME(getDocuments(completion:));
  76. /**
  77. * Attaches a listener for QuerySnapshot events.
  78. *
  79. * @param listener The listener to attach.
  80. *
  81. * @return A FIRListenerRegistration that can be used to remove this listener.
  82. */
  83. - (id<FIRListenerRegistration>)addSnapshotListener:(FIRQuerySnapshotBlock)listener
  84. NS_SWIFT_NAME(addSnapshotListener(_:));
  85. /**
  86. * Attaches a listener for QuerySnapshot events.
  87. *
  88. * @param options Options controlling the listener behavior.
  89. * @param listener The listener to attach.
  90. *
  91. * @return A FIRListenerRegistration that can be used to remove this listener.
  92. */
  93. // clang-format off
  94. - (id<FIRListenerRegistration>)addSnapshotListenerWithOptions:
  95. (nullable FIRQueryListenOptions *)options
  96. listener:(FIRQuerySnapshotBlock)listener
  97. NS_SWIFT_NAME(addSnapshotListener(options:listener:));
  98. // clang-format on
  99. #pragma mark - Filtering Data
  100. /**
  101. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  102. * contain the specified field and the value must be equal to the specified value.
  103. *
  104. * @param field The name of the field to compare.
  105. * @param value The value the field must be equal to.
  106. *
  107. * @return The created `FIRQuery`.
  108. */
  109. // clang-format off
  110. - (FIRQuery *)queryWhereField:(NSString *)field
  111. isEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isEqualTo:));
  112. // clang-format on
  113. /**
  114. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  115. * contain the specified field and the value must be equal to the specified value.
  116. *
  117. * @param path The path of the field to compare.
  118. * @param value The value the field must be equal to.
  119. *
  120. * @return The created `FIRQuery`.
  121. */
  122. // clang-format off
  123. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  124. isEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isEqualTo:));
  125. // clang-format on
  126. /**
  127. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  128. * contain the specified field and the value must be less than the specified value.
  129. *
  130. * @param field The name of the field to compare.
  131. * @param value The value the field must be less than.
  132. *
  133. * @return The created `FIRQuery`.
  134. */
  135. // clang-format off
  136. - (FIRQuery *)queryWhereField:(NSString *)field
  137. isLessThan:(id)value NS_SWIFT_NAME(whereField(_:isLessThan:));
  138. // clang-format on
  139. /**
  140. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  141. * contain the specified field and the value must be less than the specified value.
  142. *
  143. * @param path The path of the field to compare.
  144. * @param value The value the field must be less than.
  145. *
  146. * @return The created `FIRQuery`.
  147. */
  148. // clang-format off
  149. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  150. isLessThan:(id)value NS_SWIFT_NAME(whereField(_:isLessThan:));
  151. // clang-format on
  152. /**
  153. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  154. * contain the specified field and the value must be less than or equal to the specified value.
  155. *
  156. * @param field The name of the field to compare
  157. * @param value The value the field must be less than or equal to.
  158. *
  159. * @return The created `FIRQuery`.
  160. */
  161. // clang-format off
  162. - (FIRQuery *)queryWhereField:(NSString *)field
  163. isLessThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isLessThanOrEqualTo:));
  164. // clang-format on
  165. /**
  166. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  167. * contain the specified field and the value must be less than or equal to the specified value.
  168. *
  169. * @param path The path of the field to compare
  170. * @param value The value the field must be less than or equal to.
  171. *
  172. * @return The created `FIRQuery`.
  173. */
  174. // clang-format off
  175. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  176. isLessThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isLessThanOrEqualTo:));
  177. // clang-format on
  178. /**
  179. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  180. * contain the specified field and the value must greater than the specified value.
  181. *
  182. * @param field The name of the field to compare
  183. * @param value The value the field must be greater than.
  184. *
  185. * @return The created `FIRQuery`.
  186. */
  187. // clang-format off
  188. - (FIRQuery *)queryWhereField:(NSString *)field
  189. isGreaterThan:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThan:));
  190. // clang-format on
  191. /**
  192. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  193. * contain the specified field and the value must greater than the specified value.
  194. *
  195. * @param path The path of the field to compare
  196. * @param value The value the field must be greater than.
  197. *
  198. * @return The created `FIRQuery`.
  199. */
  200. // clang-format off
  201. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  202. isGreaterThan:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThan:));
  203. // clang-format on
  204. /**
  205. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  206. * contain the specified field and the value must be greater than or equal to the specified value.
  207. *
  208. * @param field The name of the field to compare
  209. * @param value The value the field must be greater than.
  210. *
  211. * @return The created `FIRQuery`.
  212. */
  213. // clang-format off
  214. - (FIRQuery *)queryWhereField:(NSString *)field
  215. isGreaterThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThanOrEqualTo:));
  216. // clang-format on
  217. /**
  218. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  219. * contain the specified field and the value must be greater than or equal to the specified value.
  220. *
  221. * @param path The path of the field to compare
  222. * @param value The value the field must be greater than.
  223. *
  224. * @return The created `FIRQuery`.
  225. */
  226. // clang-format off
  227. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  228. isGreaterThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThanOrEqualTo:));
  229. // clang-format on
  230. #pragma mark - Sorting Data
  231. /**
  232. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field.
  233. *
  234. * @param field The field to sort by.
  235. *
  236. * @return The created `FIRQuery`.
  237. */
  238. - (FIRQuery *)queryOrderedByField:(NSString *)field NS_SWIFT_NAME(order(by:));
  239. /**
  240. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field.
  241. *
  242. * @param path The field to sort by.
  243. *
  244. * @return The created `FIRQuery`.
  245. */
  246. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)path NS_SWIFT_NAME(order(by:));
  247. /**
  248. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field,
  249. * optionally in descending order instead of ascending.
  250. *
  251. * @param field The field to sort by.
  252. * @param descending Whether to sort descending.
  253. *
  254. * @return The created `FIRQuery`.
  255. */
  256. // clang-format off
  257. - (FIRQuery *)queryOrderedByField:(NSString *)field
  258. descending:(BOOL)descending NS_SWIFT_NAME(order(by:descending:));
  259. // clang-format on
  260. /**
  261. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field,
  262. * optionally in descending order instead of ascending.
  263. *
  264. * @param path The field to sort by.
  265. * @param descending Whether to sort descending.
  266. *
  267. * @return The created `FIRQuery`.
  268. */
  269. // clang-format off
  270. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)path
  271. descending:(BOOL)descending NS_SWIFT_NAME(order(by:descending:));
  272. // clang-format on
  273. #pragma mark - Limiting Data
  274. /**
  275. * Creates and returns a new `FIRQuery` that's additionally limited to only return up to
  276. * the specified number of documents.
  277. *
  278. * @param limit The maximum number of items to return.
  279. *
  280. * @return The created `FIRQuery`.
  281. */
  282. - (FIRQuery *)queryLimitedTo:(NSInteger)limit NS_SWIFT_NAME(limit(to:));
  283. #pragma mark - Choosing Endpoints
  284. /**
  285. * Creates and returns a new `FIRQuery` that starts at the provided document (inclusive). The
  286. * starting position is relative to the order of the query. The document must contain all of the
  287. * fields provided in the orderBy of this query.
  288. *
  289. * @param document The snapshot of the document to start at.
  290. *
  291. * @return The created `FIRQuery`.
  292. */
  293. - (FIRQuery *)queryStartingAtDocument:(FIRDocumentSnapshot *)document
  294. NS_SWIFT_NAME(start(atDocument:));
  295. /**
  296. * Creates and returns a new `FIRQuery` that starts at the provided fields relative to the order of
  297. * the query. The order of the field values must match the order of the order by clauses of the
  298. * query.
  299. *
  300. * @param fieldValues The field values to start this query at, in order of the query's order by.
  301. *
  302. * @return The created `FIRQuery`.
  303. */
  304. - (FIRQuery *)queryStartingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(at:));
  305. /**
  306. * Creates and returns a new `FIRQuery` that starts after the provided document (exclusive). The
  307. * starting position is relative to the order of the query. The document must contain all of the
  308. * fields provided in the orderBy of this query.
  309. *
  310. * @param document The snapshot of the document to start after.
  311. *
  312. * @return The created `FIRQuery`.
  313. */
  314. - (FIRQuery *)queryStartingAfterDocument:(FIRDocumentSnapshot *)document
  315. NS_SWIFT_NAME(start(afterDocument:));
  316. /**
  317. * Creates and returns a new `FIRQuery` that starts after the provided fields relative to the order
  318. * of the query. The order of the field values must match the order of the order by clauses of the
  319. * query.
  320. *
  321. * @param fieldValues The field values to start this query after, in order of the query's order
  322. * by.
  323. *
  324. * @return The created `FIRQuery`.
  325. */
  326. - (FIRQuery *)queryStartingAfterValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(after:));
  327. /**
  328. * Creates and returns a new `FIRQuery` that ends before the provided document (exclusive). The end
  329. * position is relative to the order of the query. The document must contain all of the fields
  330. * provided in the orderBy of this query.
  331. *
  332. * @param document The snapshot of the document to end before.
  333. *
  334. * @return The created `FIRQuery`.
  335. */
  336. - (FIRQuery *)queryEndingBeforeDocument:(FIRDocumentSnapshot *)document
  337. NS_SWIFT_NAME(end(beforeDocument:));
  338. /**
  339. * Creates and returns a new `FIRQuery` that ends before the provided fields relative to the order
  340. * of the query. The order of the field values must match the order of the order by clauses of the
  341. * query.
  342. *
  343. * @param fieldValues The field values to end this query before, in order of the query's order by.
  344. *
  345. * @return The created `FIRQuery`.
  346. */
  347. - (FIRQuery *)queryEndingBeforeValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(before:));
  348. /**
  349. * Creates and returns a new `FIRQuery` that ends at the provided document (exclusive). The end
  350. * position is relative to the order of the query. The document must contain all of the fields
  351. * provided in the orderBy of this query.
  352. *
  353. * @param document The snapshot of the document to end at.
  354. *
  355. * @return The created `FIRQuery`.
  356. */
  357. - (FIRQuery *)queryEndingAtDocument:(FIRDocumentSnapshot *)document NS_SWIFT_NAME(end(atDocument:));
  358. /**
  359. * Creates and returns a new `FIRQuery` that ends at the provided fields relative to the order of
  360. * the query. The order of the field values must match the order of the order by clauses of the
  361. * query.
  362. *
  363. * @param fieldValues The field values to end this query at, in order of the query's order by.
  364. *
  365. * @return The created `FIRQuery`.
  366. */
  367. - (FIRQuery *)queryEndingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(at:));
  368. @end
  369. NS_ASSUME_NONNULL_END