FIRQuery.h 21 KB

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