FIRQuery.h 21 KB

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