FIRQuery.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 "FIRFirestoreSource.h"
  18. #import "FIRListenerRegistration.h"
  19. @class FIRFieldPath;
  20. @class FIRFirestore;
  21. @class FIRQuerySnapshot;
  22. @class FIRDocumentSnapshot;
  23. NS_ASSUME_NONNULL_BEGIN
  24. typedef void (^FIRQuerySnapshotBlock)(FIRQuerySnapshot *_Nullable snapshot,
  25. NSError *_Nullable error);
  26. /**
  27. * A `FIRQuery` refers to a Query which you can read or listen to. You can also construct
  28. * refined `FIRQuery` objects by adding filters and ordering.
  29. */
  30. NS_SWIFT_NAME(Query)
  31. @interface FIRQuery : NSObject
  32. /** :nodoc: */
  33. - (id)init __attribute__((unavailable("FIRQuery cannot be created directly.")));
  34. /** The `FIRFirestore` for the Firestore database (useful for performing transactions, etc.). */
  35. @property(nonatomic, strong, readonly) FIRFirestore *firestore;
  36. #pragma mark - Retrieving Data
  37. /**
  38. * Reads the documents matching this query.
  39. *
  40. * This method attempts to provide up-to-date data when possible by waiting for
  41. * data from the server, but it may return cached data or fail if you are
  42. * offline and the server cannot be reached. See the
  43. * `getDocuments(source:completion:)` method to change this behavior.
  44. *
  45. * @param completion a block to execute once the documents have been successfully read.
  46. * documentSet will be `nil` only if error is `non-nil`.
  47. */
  48. - (void)getDocumentsWithCompletion:(FIRQuerySnapshotBlock)completion
  49. NS_SWIFT_NAME(getDocuments(completion:));
  50. /**
  51. * Reads the documents matching this query.
  52. *
  53. * @param source indicates whether the results should be fetched from the cache
  54. * only (`Source.cache`), the server only (`Source.server`), or to attempt
  55. * the server and fall back to the cache (`Source.default`).
  56. * @param completion a block to execute once the documents have been successfully read.
  57. * documentSet will be `nil` only if error is `non-nil`.
  58. */
  59. // clang-format off
  60. - (void)getDocumentsWithSource:(FIRFirestoreSource)source
  61. completion:(FIRQuerySnapshotBlock)completion
  62. NS_SWIFT_NAME(getDocuments(source:completion:));
  63. // clang-format on
  64. /**
  65. * Attaches a listener for QuerySnapshot events.
  66. *
  67. * @param listener The listener to attach.
  68. *
  69. * @return A FIRListenerRegistration that can be used to remove this listener.
  70. */
  71. - (id<FIRListenerRegistration>)addSnapshotListener:(FIRQuerySnapshotBlock)listener
  72. NS_SWIFT_NAME(addSnapshotListener(_:));
  73. /**
  74. * Attaches a listener for QuerySnapshot events.
  75. *
  76. * @param includeMetadataChanges Whether metadata-only changes (i.e. only
  77. * `FIRDocumentSnapshot.metadata` changed) should trigger snapshot events.
  78. * @param listener The listener to attach.
  79. *
  80. * @return A FIRListenerRegistration that can be used to remove this listener.
  81. */
  82. // clang-format off
  83. - (id<FIRListenerRegistration>)
  84. addSnapshotListenerWithIncludeMetadataChanges:(BOOL)includeMetadataChanges
  85. listener:(FIRQuerySnapshotBlock)listener
  86. NS_SWIFT_NAME(addSnapshotListener(includeMetadataChanges:listener:));
  87. // clang-format on
  88. #pragma mark - Filtering Data
  89. /**
  90. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  91. * contain the specified field and the value must be equal to the specified value.
  92. *
  93. * @param field The name of the field to compare.
  94. * @param value The value the field must be equal to.
  95. *
  96. * @return The created `FIRQuery`.
  97. */
  98. // clang-format off
  99. - (FIRQuery *)queryWhereField:(NSString *)field
  100. isEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isEqualTo:));
  101. // clang-format on
  102. /**
  103. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  104. * contain the specified field and the value must be equal to the specified value.
  105. *
  106. * @param path The path of the field to compare.
  107. * @param value The value the field must be equal to.
  108. *
  109. * @return The created `FIRQuery`.
  110. */
  111. // clang-format off
  112. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  113. isEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isEqualTo:));
  114. // clang-format on
  115. /**
  116. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  117. * contain the specified field and the value must be less than the specified value.
  118. *
  119. * @param field The name of the field to compare.
  120. * @param value The value the field must be less than.
  121. *
  122. * @return The created `FIRQuery`.
  123. */
  124. // clang-format off
  125. - (FIRQuery *)queryWhereField:(NSString *)field
  126. isLessThan:(id)value NS_SWIFT_NAME(whereField(_:isLessThan:));
  127. // clang-format on
  128. /**
  129. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  130. * contain the specified field and the value must be less than the specified value.
  131. *
  132. * @param path The path of the field to compare.
  133. * @param value The value the field must be less than.
  134. *
  135. * @return The created `FIRQuery`.
  136. */
  137. // clang-format off
  138. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  139. isLessThan:(id)value NS_SWIFT_NAME(whereField(_:isLessThan:));
  140. // clang-format on
  141. /**
  142. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  143. * contain the specified field and the value must be less than or equal to the specified value.
  144. *
  145. * @param field The name of the field to compare
  146. * @param value The value the field must be less than or equal to.
  147. *
  148. * @return The created `FIRQuery`.
  149. */
  150. // clang-format off
  151. - (FIRQuery *)queryWhereField:(NSString *)field
  152. isLessThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isLessThanOrEqualTo:));
  153. // clang-format on
  154. /**
  155. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  156. * contain the specified field and the value must be less than or equal to the specified value.
  157. *
  158. * @param path The path of the field to compare
  159. * @param value The value the field must be less than or equal to.
  160. *
  161. * @return The created `FIRQuery`.
  162. */
  163. // clang-format off
  164. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  165. isLessThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isLessThanOrEqualTo:));
  166. // clang-format on
  167. /**
  168. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  169. * contain the specified field and the value must greater than the specified value.
  170. *
  171. * @param field The name of the field to compare
  172. * @param value The value the field must be greater than.
  173. *
  174. * @return The created `FIRQuery`.
  175. */
  176. // clang-format off
  177. - (FIRQuery *)queryWhereField:(NSString *)field
  178. isGreaterThan:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThan:));
  179. // clang-format on
  180. /**
  181. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  182. * contain the specified field and the value must greater than the specified value.
  183. *
  184. * @param path The path of the field to compare
  185. * @param value The value the field must be greater than.
  186. *
  187. * @return The created `FIRQuery`.
  188. */
  189. // clang-format off
  190. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  191. isGreaterThan:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThan:));
  192. // clang-format on
  193. /**
  194. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  195. * contain the specified field and the value must be greater than or equal to the specified value.
  196. *
  197. * @param field The name of the field to compare
  198. * @param value The value the field must be greater than.
  199. *
  200. * @return The created `FIRQuery`.
  201. */
  202. // clang-format off
  203. - (FIRQuery *)queryWhereField:(NSString *)field
  204. isGreaterThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThanOrEqualTo:));
  205. // clang-format on
  206. /**
  207. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  208. * contain the specified field and the value must be greater than or equal to the specified value.
  209. *
  210. * @param path The path of the field to compare
  211. * @param value The value the field must be greater than.
  212. *
  213. * @return The created `FIRQuery`.
  214. */
  215. // clang-format off
  216. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  217. isGreaterThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThanOrEqualTo:));
  218. // clang-format on
  219. /**
  220. * Creates and returns a new `FIRQuery` with the additional filter that documents must contain
  221. * the specified field, it must be an array, and the array must contain the provided value.
  222. *
  223. * A query can have only one arrayContains filter.
  224. *
  225. * @param field The name of the field containing an array to search
  226. * @param value The value that must be contained in the array
  227. *
  228. * @return The created `FIRQuery`.
  229. */
  230. // clang-format off
  231. - (FIRQuery *)queryWhereField:(NSString *)field
  232. arrayContains:(id)value NS_SWIFT_NAME(whereField(_:arrayContains:));
  233. // clang-format on
  234. /**
  235. * Creates and returns a new `FIRQuery` with the additional filter that documents must contain
  236. * the specified field, it must be an array, and the array must contain the provided value.
  237. *
  238. * A query can have only one arrayContains filter.
  239. *
  240. * @param path The path of the field containing an array to search
  241. * @param value The value that must be contained in the array
  242. *
  243. * @return The created `FIRQuery`.
  244. */
  245. // clang-format off
  246. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  247. arrayContains:(id)value NS_SWIFT_NAME(whereField(_:arrayContains:));
  248. // clang-format on
  249. /**
  250. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  251. * satisfy the specified predicate.
  252. *
  253. * @param predicate The predicate the document must satisfy. Can be either comparison
  254. * or compound of comparison. In particular, block-based predicate is not supported.
  255. *
  256. * @return The created `FIRQuery`.
  257. */
  258. // clang-format off
  259. - (FIRQuery *)queryFilteredUsingPredicate:(NSPredicate *)predicate NS_SWIFT_NAME(filter(using:));
  260. // clang-format on
  261. #pragma mark - Sorting Data
  262. /**
  263. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field.
  264. *
  265. * @param field The field to sort by.
  266. *
  267. * @return The created `FIRQuery`.
  268. */
  269. - (FIRQuery *)queryOrderedByField:(NSString *)field NS_SWIFT_NAME(order(by:));
  270. /**
  271. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field.
  272. *
  273. * @param path The field to sort by.
  274. *
  275. * @return The created `FIRQuery`.
  276. */
  277. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)path NS_SWIFT_NAME(order(by:));
  278. /**
  279. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field,
  280. * optionally in descending order instead of ascending.
  281. *
  282. * @param field The field to sort by.
  283. * @param descending Whether to sort descending.
  284. *
  285. * @return The created `FIRQuery`.
  286. */
  287. // clang-format off
  288. - (FIRQuery *)queryOrderedByField:(NSString *)field
  289. descending:(BOOL)descending NS_SWIFT_NAME(order(by:descending:));
  290. // clang-format on
  291. /**
  292. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field,
  293. * optionally in descending order instead of ascending.
  294. *
  295. * @param path The field to sort by.
  296. * @param descending Whether to sort descending.
  297. *
  298. * @return The created `FIRQuery`.
  299. */
  300. // clang-format off
  301. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)path
  302. descending:(BOOL)descending NS_SWIFT_NAME(order(by:descending:));
  303. // clang-format on
  304. #pragma mark - Limiting Data
  305. /**
  306. * Creates and returns a new `FIRQuery` that's additionally limited to only return up to
  307. * the specified number of documents.
  308. *
  309. * @param limit The maximum number of items to return.
  310. *
  311. * @return The created `FIRQuery`.
  312. */
  313. - (FIRQuery *)queryLimitedTo:(NSInteger)limit NS_SWIFT_NAME(limit(to:));
  314. #pragma mark - Choosing Endpoints
  315. /**
  316. * Creates and returns a new `FIRQuery` that starts at the provided document (inclusive). The
  317. * starting position is relative to the order of the query. The document must contain all of the
  318. * fields provided in the orderBy of this query.
  319. *
  320. * @param document The snapshot of the document to start at.
  321. *
  322. * @return The created `FIRQuery`.
  323. */
  324. - (FIRQuery *)queryStartingAtDocument:(FIRDocumentSnapshot *)document
  325. NS_SWIFT_NAME(start(atDocument:));
  326. /**
  327. * Creates and returns a new `FIRQuery` that starts at the provided fields relative to the order of
  328. * the query. The order of the field values must match the order of the order by clauses of the
  329. * query.
  330. *
  331. * @param fieldValues The field values to start this query at, in order of the query's order by.
  332. *
  333. * @return The created `FIRQuery`.
  334. */
  335. - (FIRQuery *)queryStartingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(at:));
  336. /**
  337. * Creates and returns a new `FIRQuery` that starts after the provided document (exclusive). The
  338. * starting position is relative to the order of the query. The document must contain all of the
  339. * fields provided in the orderBy of this query.
  340. *
  341. * @param document The snapshot of the document to start after.
  342. *
  343. * @return The created `FIRQuery`.
  344. */
  345. - (FIRQuery *)queryStartingAfterDocument:(FIRDocumentSnapshot *)document
  346. NS_SWIFT_NAME(start(afterDocument:));
  347. /**
  348. * Creates and returns a new `FIRQuery` that starts after the provided fields relative to the order
  349. * of the query. The order of the field values must match the order of the order by clauses of the
  350. * query.
  351. *
  352. * @param fieldValues The field values to start this query after, in order of the query's order
  353. * by.
  354. *
  355. * @return The created `FIRQuery`.
  356. */
  357. - (FIRQuery *)queryStartingAfterValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(after:));
  358. /**
  359. * Creates and returns a new `FIRQuery` that ends before the provided document (exclusive). The end
  360. * position is relative to the order of the query. The document must contain all of the fields
  361. * provided in the orderBy of this query.
  362. *
  363. * @param document The snapshot of the document to end before.
  364. *
  365. * @return The created `FIRQuery`.
  366. */
  367. - (FIRQuery *)queryEndingBeforeDocument:(FIRDocumentSnapshot *)document
  368. NS_SWIFT_NAME(end(beforeDocument:));
  369. /**
  370. * Creates and returns a new `FIRQuery` that ends before the provided fields relative to the order
  371. * of the query. The order of the field values must match the order of the order by clauses of the
  372. * query.
  373. *
  374. * @param fieldValues The field values to end this query before, in order of the query's order by.
  375. *
  376. * @return The created `FIRQuery`.
  377. */
  378. - (FIRQuery *)queryEndingBeforeValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(before:));
  379. /**
  380. * Creates and returns a new `FIRQuery` that ends at the provided document (exclusive). The end
  381. * position is relative to the order of the query. The document must contain all of the fields
  382. * provided in the orderBy of this query.
  383. *
  384. * @param document The snapshot of the document to end at.
  385. *
  386. * @return The created `FIRQuery`.
  387. */
  388. - (FIRQuery *)queryEndingAtDocument:(FIRDocumentSnapshot *)document NS_SWIFT_NAME(end(atDocument:));
  389. /**
  390. * Creates and returns a new `FIRQuery` that ends at the provided fields relative to the order of
  391. * the query. The order of the field values must match the order of the order by clauses of the
  392. * query.
  393. *
  394. * @param fieldValues The field values to end this query at, in order of the query's order by.
  395. *
  396. * @return The created `FIRQuery`.
  397. */
  398. - (FIRQuery *)queryEndingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(at:));
  399. @end
  400. NS_ASSUME_NONNULL_END