FIRQuery.h 20 KB

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