FIRQuery.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 FIRAggregateQuery;
  20. @class FIRFieldPath;
  21. @class FIRFirestore;
  22. @class FIRFilter;
  23. @class FIRQuerySnapshot;
  24. @class FIRDocumentSnapshot;
  25. NS_ASSUME_NONNULL_BEGIN
  26. /**
  27. * A block type used to handle failable snapshot method callbacks.
  28. */
  29. typedef void (^FIRQuerySnapshotBlock)(FIRQuerySnapshot *_Nullable snapshot,
  30. NSError *_Nullable error)
  31. NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
  32. /**
  33. * A `Query` refers to a query which you can read or listen to. You can also construct
  34. * refined `Query` objects by adding filters and ordering.
  35. */
  36. NS_SWIFT_NAME(Query)
  37. @interface FIRQuery : NSObject
  38. /** :nodoc: */
  39. - (id)init __attribute__((unavailable("FIRQuery cannot be created directly.")));
  40. /** The `Firestore` instance that created this query (useful for performing transactions, etc.). */
  41. @property(nonatomic, strong, readonly) FIRFirestore *firestore;
  42. #pragma mark - Retrieving Data
  43. /**
  44. * Reads the documents matching this query.
  45. *
  46. * This method attempts to provide up-to-date data when possible by waiting for
  47. * data from the server, but it may return cached data or fail if you are
  48. * offline and the server cannot be reached. See the
  49. * `getDocuments(source:completion:)` method to change this behavior.
  50. *
  51. * @param completion a block to execute once the documents have been successfully read.
  52. * documentSet will be `nil` only if error is `non-nil`.
  53. */
  54. - (void)getDocumentsWithCompletion:
  55. (void (^)(FIRQuerySnapshot *_Nullable snapshot, NSError *_Nullable error))completion
  56. NS_SWIFT_NAME(getDocuments(completion:));
  57. /**
  58. * Reads the documents matching this query.
  59. *
  60. * @param source indicates whether the results should be fetched from the cache
  61. * only (`Source.cache`), the server only (`Source.server`), or to attempt
  62. * the server and fall back to the cache (`Source.default`).
  63. * @param completion a block to execute once the documents have been successfully read.
  64. * documentSet will be `nil` only if error is `non-nil`.
  65. */
  66. - (void)getDocumentsWithSource:(FIRFirestoreSource)source
  67. completion:(void (^)(FIRQuerySnapshot *_Nullable snapshot,
  68. NSError *_Nullable error))completion
  69. NS_SWIFT_NAME(getDocuments(source:completion:));
  70. /**
  71. * Attaches a listener for `QuerySnapshot` events.
  72. *
  73. * @param listener The listener to attach.
  74. *
  75. * @return A `ListenerRegistration` object that can be used to remove this listener.
  76. */
  77. - (id<FIRListenerRegistration>)addSnapshotListener:
  78. (void (^)(FIRQuerySnapshot *_Nullable snapshot, NSError *_Nullable error))listener
  79. NS_SWIFT_NAME(addSnapshotListener(_:));
  80. /**
  81. * Attaches a listener for `QuerySnapshot` events.
  82. *
  83. * @param includeMetadataChanges Whether metadata-only changes (i.e. only
  84. * `DocumentSnapshot.metadata` changed) should trigger snapshot events.
  85. * @param listener The listener to attach.
  86. *
  87. * @return A `ListenerRegistration` that can be used to remove this listener.
  88. */
  89. - (id<FIRListenerRegistration>)
  90. addSnapshotListenerWithIncludeMetadataChanges:(BOOL)includeMetadataChanges
  91. listener:(void (^)(FIRQuerySnapshot *_Nullable snapshot,
  92. NSError *_Nullable error))listener
  93. NS_SWIFT_NAME(addSnapshotListener(includeMetadataChanges:listener:));
  94. #pragma mark - Filtering Data
  95. /**
  96. * Creates and returns a new Query with the additional filter.
  97. *
  98. * @param filter The new filter to apply to the existing query.
  99. * @return The newly created Query.
  100. */
  101. - (FIRQuery *)queryWhereFilter:(FIRFilter *)filter NS_SWIFT_NAME(whereFilter(_:));
  102. /**
  103. * Creates and returns a new `Query` 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 field The name of the field to compare.
  107. * @param value The value the field must be equal to.
  108. *
  109. * @return The created `Query`.
  110. */
  111. - (FIRQuery *)queryWhereField:(NSString *)field
  112. isEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isEqualTo:));
  113. /**
  114. * Creates and returns a new `Query` with the additional filter that documents must
  115. * contain the specified field and the value does not equal 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 `Query`.
  121. */
  122. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  123. isNotEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isNotEqualTo:));
  124. /**
  125. * Creates and returns a new `Query` with the additional filter that documents must
  126. * contain the specified field and the value does not equal the specified value.
  127. *
  128. * @param field The name of the field to compare.
  129. * @param value The value the field must be equal to.
  130. *
  131. * @return The created `Query`.
  132. */
  133. - (FIRQuery *)queryWhereField:(NSString *)field
  134. isNotEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isNotEqualTo:));
  135. /**
  136. * Creates and returns a new `Query` with the additional filter that documents must
  137. * contain the specified field and the value must be equal to the specified value.
  138. *
  139. * @param path The path of the field to compare.
  140. * @param value The value the field must be equal to.
  141. *
  142. * @return The created `Query`.
  143. */
  144. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  145. isEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isEqualTo:));
  146. /**
  147. * Creates and returns a new `Query` with the additional filter that documents must
  148. * contain the specified field and the value must be less than the specified value.
  149. *
  150. * @param field The name of the field to compare.
  151. * @param value The value the field must be less than.
  152. *
  153. * @return The created `Query`.
  154. */
  155. - (FIRQuery *)queryWhereField:(NSString *)field
  156. isLessThan:(id)value NS_SWIFT_NAME(whereField(_:isLessThan:));
  157. /**
  158. * Creates and returns a new `Query` with the additional filter that documents must
  159. * contain the specified field and the value must be less than the specified value.
  160. *
  161. * @param path The path of the field to compare.
  162. * @param value The value the field must be less than.
  163. *
  164. * @return The created `Query`.
  165. */
  166. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  167. isLessThan:(id)value NS_SWIFT_NAME(whereField(_:isLessThan:));
  168. /**
  169. * Creates and returns a new `Query` with the additional filter that documents must
  170. * contain the specified field and the value must be less than or equal to the specified value.
  171. *
  172. * @param field The name of the field to compare
  173. * @param value The value the field must be less than or equal to.
  174. *
  175. * @return The created `Query`.
  176. */
  177. - (FIRQuery *)queryWhereField:(NSString *)field
  178. isLessThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isLessThanOrEqualTo:));
  179. /**
  180. * Creates and returns a new `Query` with the additional filter that documents must
  181. * contain the specified field and the value must be less than or equal to the specified value.
  182. *
  183. * @param path The path of the field to compare
  184. * @param value The value the field must be less than or equal to.
  185. *
  186. * @return The created `Query`.
  187. */
  188. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  189. isLessThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isLessThanOrEqualTo:));
  190. /**
  191. * Creates and returns a new `Query` with the additional filter that documents must
  192. * contain the specified field and the value must greater than the specified value.
  193. *
  194. * @param field The name of the field to compare
  195. * @param value The value the field must be greater than.
  196. *
  197. * @return The created `Query`.
  198. */
  199. - (FIRQuery *)queryWhereField:(NSString *)field
  200. isGreaterThan:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThan:));
  201. /**
  202. * Creates and returns a new `Query` with the additional filter that documents must
  203. * contain the specified field and the value must greater than the specified value.
  204. *
  205. * @param path The path of the field to compare
  206. * @param value The value the field must be greater than.
  207. *
  208. * @return The created `Query`.
  209. */
  210. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  211. isGreaterThan:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThan:));
  212. /**
  213. * Creates and returns a new `Query` with the additional filter that documents must
  214. * contain the specified field and the value must be greater than or equal to the specified value.
  215. *
  216. * @param field The name of the field to compare
  217. * @param value The value the field must be greater than.
  218. *
  219. * @return The created `Query`.
  220. */
  221. - (FIRQuery *)queryWhereField:(NSString *)field
  222. isGreaterThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThanOrEqualTo:));
  223. /**
  224. * Creates and returns a new `Query` with the additional filter that documents must
  225. * contain the specified field and the value must be greater than or equal to the specified value.
  226. *
  227. * @param path The path of the field to compare
  228. * @param value The value the field must be greater than.
  229. *
  230. * @return The created `Query`.
  231. */
  232. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  233. isGreaterThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThanOrEqualTo:));
  234. /**
  235. * Creates and returns a new `Query` 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 field The name 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 `Query`.
  244. */
  245. - (FIRQuery *)queryWhereField:(NSString *)field
  246. arrayContains:(id)value NS_SWIFT_NAME(whereField(_:arrayContains:));
  247. /**
  248. * Creates and returns a new `Query` with the additional filter that documents must contain
  249. * the specified field, it must be an array, and the array must contain the provided value.
  250. *
  251. * A query can have only one `arrayContains` filter.
  252. *
  253. * @param path The path of the field containing an array to search
  254. * @param value The value that must be contained in the array
  255. *
  256. * @return The created `Query`.
  257. */
  258. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  259. arrayContains:(id)value NS_SWIFT_NAME(whereField(_:arrayContains:));
  260. /**
  261. * Creates and returns a new `Query` with the additional filter that documents must contain
  262. * the specified field, the value must be an array, and that array must contain at least one value
  263. * from the provided array.
  264. *
  265. * A query can have only one `arrayContainsAny` filter and it cannot be combined with
  266. * `arrayContains` or `in` filters.
  267. *
  268. * @param field The name of the field containing an array to search.
  269. * @param values The array that contains the values to match.
  270. *
  271. * @return The created `Query`.
  272. */
  273. - (FIRQuery *)queryWhereField:(NSString *)field
  274. arrayContainsAny:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:arrayContainsAny:));
  275. /**
  276. * Creates and returns a new `Query` with the additional filter that documents must contain
  277. * the specified field, the value must be an array, and that array must contain at least one value
  278. * from the provided array.
  279. *
  280. * A query can have only one `arrayContainsAny` filter and it cannot be combined with
  281. * `arrayContains` or `in` filters.
  282. *
  283. * @param path The path of the field containing an array to search.
  284. * @param values The array that contains the values to match.
  285. *
  286. * @return The created `Query`.
  287. */
  288. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  289. arrayContainsAny:(NSArray<id> *)values
  290. NS_SWIFT_NAME(whereField(_:arrayContainsAny:));
  291. /**
  292. * Creates and returns a new `Query` with the additional filter that documents must contain
  293. * the specified field and the value must equal one of the values from the provided array.
  294. *
  295. * A query can have only one `in` filter, and it cannot be combined with an `arrayContainsAny`
  296. * filter.
  297. *
  298. * @param field The name of the field to search.
  299. * @param values The array that contains the values to match.
  300. *
  301. * @return The created `Query`.
  302. */
  303. - (FIRQuery *)queryWhereField:(NSString *)field
  304. in:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:in:));
  305. /**
  306. * Creates and returns a new `Query` with the additional filter that documents must contain
  307. * the specified field and the value must equal one of the values from the provided array.
  308. *
  309. * A query can have only one `in` filter, and it cannot be combined with an `arrayContainsAny`
  310. * filter.
  311. *
  312. * @param path The path of the field to search.
  313. * @param values The array that contains the values to match.
  314. *
  315. * @return The created `Query`.
  316. */
  317. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  318. in:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:in:));
  319. /**
  320. * Creates and returns a new `Query` with the additional filter that documents must contain
  321. * the specified field and the value does not equal any of the values from the provided array.
  322. *
  323. * One special case is that `notIn` filters cannot match `nil` values. To query for documents
  324. * where a field exists and is `nil`, use a `notEqual` filter, which can handle this special case.
  325. *
  326. * A query can have only one `notIn` filter, and it cannot be combined with an `arrayContains`,
  327. * `arrayContainsAny`, `in`, or `notEqual` filter.
  328. *
  329. * @param field The name of the field to search.
  330. * @param values The array that contains the values to match.
  331. *
  332. * @return The created `Query`.
  333. */
  334. - (FIRQuery *)queryWhereField:(NSString *)field
  335. notIn:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:notIn:));
  336. /**
  337. * Creates and returns a new `Query` with the additional filter that documents must contain
  338. * the specified field and the value does not equal any of the values from the provided array.
  339. *
  340. * One special case is that `notIn` filters cannot match `nil` values. To query for documents
  341. * where a field exists and is `nil`, use a `notEqual` filter, which can handle this special case.
  342. *
  343. * Passing in a `null` value into the `values` array results in no document matches. To query
  344. * for documents where a field is not `null`, use a `notEqual` filter.
  345. *
  346. * @param path The path of the field to search.
  347. * @param values The array that contains the values to match.
  348. *
  349. * @return The created `Query`.
  350. */
  351. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  352. notIn:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:notIn:));
  353. /**
  354. * Creates and returns a new `Query` with the additional filter that documents must
  355. * satisfy the specified predicate.
  356. *
  357. * @param predicate The predicate the document must satisfy. Can be either comparison
  358. * or compound of comparison. In particular, block-based predicate is not supported.
  359. *
  360. * @return The created `Query`.
  361. */
  362. - (FIRQuery *)queryFilteredUsingPredicate:(NSPredicate *)predicate NS_SWIFT_NAME(filter(using:));
  363. #pragma mark - Sorting Data
  364. /**
  365. * Creates and returns a new `Query` that's additionally sorted by the specified field.
  366. *
  367. * @param field The field to sort by.
  368. *
  369. * @return The created `Query`.
  370. */
  371. - (FIRQuery *)queryOrderedByField:(NSString *)field NS_SWIFT_NAME(order(by:));
  372. /**
  373. * Creates and returns a new `Query` that's additionally sorted by the specified field.
  374. *
  375. * @param path The field to sort by.
  376. *
  377. * @return The created `Query`.
  378. */
  379. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)path NS_SWIFT_NAME(order(by:));
  380. /**
  381. * Creates and returns a new `Query` that's additionally sorted by the specified field,
  382. * optionally in descending order instead of ascending.
  383. *
  384. * @param field The field to sort by.
  385. * @param descending Whether to sort descending.
  386. *
  387. * @return The created `Query`.
  388. */
  389. - (FIRQuery *)queryOrderedByField:(NSString *)field
  390. descending:(BOOL)descending NS_SWIFT_NAME(order(by:descending:));
  391. /**
  392. * Creates and returns a new `Query` that's additionally sorted by the specified field,
  393. * optionally in descending order instead of ascending.
  394. *
  395. * @param path The field to sort by.
  396. * @param descending Whether to sort descending.
  397. *
  398. * @return The created `Query`.
  399. */
  400. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)path
  401. descending:(BOOL)descending NS_SWIFT_NAME(order(by:descending:));
  402. #pragma mark - Limiting Data
  403. /**
  404. * Creates and returns a new `Query` that only returns the first matching documents up to
  405. * the specified number.
  406. *
  407. * @param limit The maximum number of items to return.
  408. *
  409. * @return The created `Query`.
  410. */
  411. - (FIRQuery *)queryLimitedTo:(NSInteger)limit NS_SWIFT_NAME(limit(to:));
  412. /**
  413. * Creates and returns a new `Query` that only returns the last matching documents up to
  414. * the specified number.
  415. *
  416. * A query with a `limit(toLast:)` clause must have at least one `orderBy` clause.
  417. *
  418. * @param limit The maximum number of items to return.
  419. *
  420. * @return The created `Query`.
  421. */
  422. - (FIRQuery *)queryLimitedToLast:(NSInteger)limit NS_SWIFT_NAME(limit(toLast:));
  423. #pragma mark - Choosing Endpoints
  424. /**
  425. * Creates and returns a new `Query` that starts at the provided document (inclusive). The
  426. * starting position is relative to the order of the query. The document must contain all of the
  427. * fields provided in the orderBy of this query.
  428. *
  429. * @param document The snapshot of the document to start at.
  430. *
  431. * @return The created `Query`.
  432. */
  433. - (FIRQuery *)queryStartingAtDocument:(FIRDocumentSnapshot *)document
  434. NS_SWIFT_NAME(start(atDocument:));
  435. /**
  436. * Creates and returns a new `Query` that starts at the provided fields relative to the order of
  437. * the query. The order of the field values must match the order of the order by clauses of the
  438. * query.
  439. *
  440. * @param fieldValues The field values to start this query at, in order of the query's order by.
  441. *
  442. * @return The created `Query`.
  443. */
  444. - (FIRQuery *)queryStartingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(at:));
  445. /**
  446. * Creates and returns a new `Query` that starts after the provided document (exclusive). The
  447. * starting position is relative to the order of the query. The document must contain all of the
  448. * fields provided in the orderBy of this query.
  449. *
  450. * @param document The snapshot of the document to start after.
  451. *
  452. * @return The created `Query`.
  453. */
  454. - (FIRQuery *)queryStartingAfterDocument:(FIRDocumentSnapshot *)document
  455. NS_SWIFT_NAME(start(afterDocument:));
  456. /**
  457. * Creates and returns a new `Query` that starts after the provided fields relative to the order
  458. * of the query. The order of the field values must match the order of the order by clauses of the
  459. * query.
  460. *
  461. * @param fieldValues The field values to start this query after, in order of the query's orderBy.
  462. *
  463. * @return The created `Query`.
  464. */
  465. - (FIRQuery *)queryStartingAfterValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(after:));
  466. /**
  467. * Creates and returns a new `Query` that ends before the provided document (exclusive). The end
  468. * position is relative to the order of the query. The document must contain all of the fields
  469. * provided in the orderBy of this query.
  470. *
  471. * @param document The snapshot of the document to end before.
  472. *
  473. * @return The created `Query`.
  474. */
  475. - (FIRQuery *)queryEndingBeforeDocument:(FIRDocumentSnapshot *)document
  476. NS_SWIFT_NAME(end(beforeDocument:));
  477. /**
  478. * Creates and returns a new `Query` that ends before the provided fields relative to the order
  479. * of the query. The order of the field values must match the order of the order by clauses of the
  480. * query.
  481. *
  482. * @param fieldValues The field values to end this query before, in order of the query's order by.
  483. *
  484. * @return The created `Query`.
  485. */
  486. - (FIRQuery *)queryEndingBeforeValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(before:));
  487. /**
  488. * Creates and returns a new `Query` that ends at the provided document (exclusive). The end
  489. * position is relative to the order of the query. The document must contain all of the fields
  490. * provided in the orderBy of this query.
  491. *
  492. * @param document The snapshot of the document to end at.
  493. *
  494. * @return The created `Query`.
  495. */
  496. - (FIRQuery *)queryEndingAtDocument:(FIRDocumentSnapshot *)document NS_SWIFT_NAME(end(atDocument:));
  497. /**
  498. * Creates and returns a new `Query` that ends at the provided fields relative to the order of
  499. * the query. The order of the field values must match the order of the order by clauses of the
  500. * query.
  501. *
  502. * @param fieldValues The field values to end this query at, in order of the query's order by.
  503. *
  504. * @return The created `Query`.
  505. */
  506. - (FIRQuery *)queryEndingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(at:));
  507. #pragma mark - Aggregation
  508. /**
  509. * A query that counts the documents in the result set of this query, without actually downloading
  510. * the documents.
  511. *
  512. * Using this `AggregateQuery` to count the documents is efficient because only the final count,
  513. * not the documents' data, is downloaded. The query can even count the documents if the result
  514. * set would be prohibitively large to download entirely (e.g. thousands of documents).
  515. */
  516. @property(nonatomic, readonly) FIRAggregateQuery *count;
  517. @end
  518. NS_ASSUME_NONNULL_END