FIRQuery.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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
  261. NS_SWIFT_NAME(whereField(_:arrayContainsAny:));
  262. /**
  263. * Creates and returns a new `FIRQuery` with the additional filter that documents must contain
  264. * the specified field, the value must be an array, and that array must contain at least one value
  265. * from the provided array.
  266. *
  267. * A query can have only one `arrayContainsAny` filter and it cannot be combined with
  268. * `arrayContains` or `in` filters.
  269. *
  270. * @param path The path of the field containing an array to search.
  271. * @param values The array that contains the values to match.
  272. *
  273. * @return The created `FIRQuery`.
  274. */
  275. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  276. arrayContainsAny:(NSArray<id> *)values
  277. NS_SWIFT_NAME(whereField(_:arrayContainsAny:));
  278. /**
  279. * Creates and returns a new `FIRQuery` with the additional filter that documents must contain
  280. * the specified field and the value must equal one of the values from the provided array.
  281. *
  282. * A query can have only one `in` filter, and it cannot be combined with an `arrayContainsAny`
  283. * filter.
  284. *
  285. * @param field The name of the field to search.
  286. * @param values The array that contains the values to match.
  287. *
  288. * @return The created `FIRQuery`.
  289. */
  290. - (FIRQuery *)queryWhereField:(NSString *)field
  291. in:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:in:));
  292. /**
  293. * Creates and returns a new `FIRQuery` with the additional filter that documents must contain
  294. * the specified field and the value must equal one of the values from the provided array.
  295. *
  296. * A query can have only one `in` filter, and it cannot be combined with an `arrayContainsAny`
  297. * filter.
  298. *
  299. * @param path The path of the field to search.
  300. * @param values The array that contains the values to match.
  301. *
  302. * @return The created `FIRQuery`.
  303. */
  304. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  305. in:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:in:));
  306. /**
  307. * Creates and returns a new `FIRQuery` with the additional filter that documents must contain
  308. * the specified field and the value does not equal any of the values from the provided array.
  309. *
  310. * One special case is that `notIn` filters cannot match `nil` values. To query for documents
  311. * where a field exists and is `nil`, use a `notEqual` filter, which can handle this special case.
  312. *
  313. * A query can have only one `notIn` filter, and it cannot be combined with an `arrayContains`,
  314. * `arrayContainsAny`, `in`, or `notEqual` filter.
  315. *
  316. * @param field The name of the field to search.
  317. * @param values The array that contains the values to match.
  318. *
  319. * @return The created `FIRQuery`.
  320. */
  321. - (FIRQuery *)queryWhereField:(NSString *)field
  322. notIn:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:notIn:));
  323. /**
  324. * Creates and returns a new `FIRQuery` with the additional filter that documents must contain
  325. * the specified field and the value does not equal any of the values from the provided array.
  326. *
  327. * One special case is that `notIn` filters cannot match `nil` values. To query for documents
  328. * where a field exists and is `nil`, use a `notEqual` filter, which can handle this special case.
  329. *
  330. * Passing in a `null` value into the `values` array results in no document matches. To query
  331. * for documents where a field is not `null`, use a `notEqual` filter.
  332. *
  333. * @param path The path of the field to search.
  334. * @param values The array that contains the values to match.
  335. *
  336. * @return The created `FIRQuery`.
  337. */
  338. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  339. notIn:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:notIn:));
  340. /**
  341. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  342. * satisfy the specified predicate.
  343. *
  344. * @param predicate The predicate the document must satisfy. Can be either comparison
  345. * or compound of comparison. In particular, block-based predicate is not supported.
  346. *
  347. * @return The created `FIRQuery`.
  348. */
  349. - (FIRQuery *)queryFilteredUsingPredicate:(NSPredicate *)predicate NS_SWIFT_NAME(filter(using:));
  350. #pragma mark - Sorting Data
  351. /**
  352. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field.
  353. *
  354. * @param field The field to sort by.
  355. *
  356. * @return The created `FIRQuery`.
  357. */
  358. - (FIRQuery *)queryOrderedByField:(NSString *)field NS_SWIFT_NAME(order(by:));
  359. /**
  360. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field.
  361. *
  362. * @param path The field to sort by.
  363. *
  364. * @return The created `FIRQuery`.
  365. */
  366. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)path NS_SWIFT_NAME(order(by:));
  367. /**
  368. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field,
  369. * optionally in descending order instead of ascending.
  370. *
  371. * @param field The field to sort by.
  372. * @param descending Whether to sort descending.
  373. *
  374. * @return The created `FIRQuery`.
  375. */
  376. - (FIRQuery *)queryOrderedByField:(NSString *)field
  377. descending:(BOOL)descending NS_SWIFT_NAME(order(by:descending:));
  378. /**
  379. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field,
  380. * optionally in descending order instead of ascending.
  381. *
  382. * @param path The field to sort by.
  383. * @param descending Whether to sort descending.
  384. *
  385. * @return The created `FIRQuery`.
  386. */
  387. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)path
  388. descending:(BOOL)descending NS_SWIFT_NAME(order(by:descending:));
  389. #pragma mark - Limiting Data
  390. /**
  391. * Creates and returns a new `FIRQuery` that only returns the first matching documents up to
  392. * the specified number.
  393. *
  394. * @param limit The maximum number of items to return.
  395. *
  396. * @return The created `FIRQuery`.
  397. */
  398. - (FIRQuery *)queryLimitedTo:(NSInteger)limit NS_SWIFT_NAME(limit(to:));
  399. /**
  400. * Creates and returns a new `FIRQuery` that only returns the last matching documents up to
  401. * the specified number.
  402. *
  403. * A query with a `limit(ToLast:)` clause must have at least one `orderBy` clause.
  404. *
  405. * @param limit The maximum number of items to return.
  406. *
  407. * @return The created `FIRQuery`.
  408. */
  409. - (FIRQuery *)queryLimitedToLast:(NSInteger)limit NS_SWIFT_NAME(limit(toLast:));
  410. #pragma mark - Choosing Endpoints
  411. /**
  412. * Creates and returns a new `FIRQuery` that starts at the provided document (inclusive). The
  413. * starting position is relative to the order of the query. The document must contain all of the
  414. * fields provided in the orderBy of this query.
  415. *
  416. * @param document The snapshot of the document to start at.
  417. *
  418. * @return The created `FIRQuery`.
  419. */
  420. - (FIRQuery *)queryStartingAtDocument:(FIRDocumentSnapshot *)document
  421. NS_SWIFT_NAME(start(atDocument:));
  422. /**
  423. * Creates and returns a new `FIRQuery` that starts at the provided fields relative to the order of
  424. * the query. The order of the field values must match the order of the order by clauses of the
  425. * query.
  426. *
  427. * @param fieldValues The field values to start this query at, in order of the query's order by.
  428. *
  429. * @return The created `FIRQuery`.
  430. */
  431. - (FIRQuery *)queryStartingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(at:));
  432. /**
  433. * Creates and returns a new `FIRQuery` that starts after the provided document (exclusive). The
  434. * starting position is relative to the order of the query. The document must contain all of the
  435. * fields provided in the orderBy of this query.
  436. *
  437. * @param document The snapshot of the document to start after.
  438. *
  439. * @return The created `FIRQuery`.
  440. */
  441. - (FIRQuery *)queryStartingAfterDocument:(FIRDocumentSnapshot *)document
  442. NS_SWIFT_NAME(start(afterDocument:));
  443. /**
  444. * Creates and returns a new `FIRQuery` that starts after the provided fields relative to the order
  445. * of the query. The order of the field values must match the order of the order by clauses of the
  446. * query.
  447. *
  448. * @param fieldValues The field values to start this query after, in order of the query's order
  449. * by.
  450. *
  451. * @return The created `FIRQuery`.
  452. */
  453. - (FIRQuery *)queryStartingAfterValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(after:));
  454. /**
  455. * Creates and returns a new `FIRQuery` that ends before the provided document (exclusive). The end
  456. * position is relative to the order of the query. The document must contain all of the fields
  457. * provided in the orderBy of this query.
  458. *
  459. * @param document The snapshot of the document to end before.
  460. *
  461. * @return The created `FIRQuery`.
  462. */
  463. - (FIRQuery *)queryEndingBeforeDocument:(FIRDocumentSnapshot *)document
  464. NS_SWIFT_NAME(end(beforeDocument:));
  465. /**
  466. * Creates and returns a new `FIRQuery` that ends before the provided fields relative to the order
  467. * of the query. The order of the field values must match the order of the order by clauses of the
  468. * query.
  469. *
  470. * @param fieldValues The field values to end this query before, in order of the query's order by.
  471. *
  472. * @return The created `FIRQuery`.
  473. */
  474. - (FIRQuery *)queryEndingBeforeValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(before:));
  475. /**
  476. * Creates and returns a new `FIRQuery` that ends at the provided document (exclusive). The end
  477. * position is relative to the order of the query. The document must contain all of the fields
  478. * provided in the orderBy of this query.
  479. *
  480. * @param document The snapshot of the document to end at.
  481. *
  482. * @return The created `FIRQuery`.
  483. */
  484. - (FIRQuery *)queryEndingAtDocument:(FIRDocumentSnapshot *)document
  485. NS_SWIFT_NAME(end(atDocument:));
  486. /**
  487. * Creates and returns a new `FIRQuery` that ends at the provided fields relative to the order of
  488. * the query. The order of the field values must match the order of the order by clauses of the
  489. * query.
  490. *
  491. * @param fieldValues The field values to end this query at, in order of the query's order by.
  492. *
  493. * @return The created `FIRQuery`.
  494. */
  495. - (FIRQuery *)queryEndingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(at:));
  496. @end
  497. NS_ASSUME_NONNULL_END