FIRQuery.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 `FIRQuery` refers to a Query which you can read or listen to. You can also construct
  32. * refined `FIRQuery` 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 `FIRFirestore` for the Firestore database (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 FIRListenerRegistration 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. * `FIRDocumentSnapshot.metadata` changed) should trigger snapshot events.
  83. * @param listener The listener to attach.
  84. *
  85. * @return A FIRListenerRegistration 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 `FIRQuery` 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 `FIRQuery`.
  101. */
  102. - (FIRQuery *)queryWhereField:(NSString *)field
  103. isEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isEqualTo:));
  104. /**
  105. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  112. */
  113. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  114. isNotEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isNotEqualTo:));
  115. /**
  116. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  123. */
  124. - (FIRQuery *)queryWhereField:(NSString *)field
  125. isNotEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isNotEqualTo:));
  126. /**
  127. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  134. */
  135. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  136. isEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isEqualTo:));
  137. /**
  138. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  145. */
  146. - (FIRQuery *)queryWhereField:(NSString *)field
  147. isLessThan:(id)value NS_SWIFT_NAME(whereField(_:isLessThan:));
  148. /**
  149. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  156. */
  157. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  158. isLessThan:(id)value NS_SWIFT_NAME(whereField(_:isLessThan:));
  159. /**
  160. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  167. */
  168. - (FIRQuery *)queryWhereField:(NSString *)field
  169. isLessThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isLessThanOrEqualTo:));
  170. /**
  171. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  178. */
  179. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  180. isLessThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isLessThanOrEqualTo:));
  181. /**
  182. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  189. */
  190. - (FIRQuery *)queryWhereField:(NSString *)field
  191. isGreaterThan:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThan:));
  192. /**
  193. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  200. */
  201. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  202. isGreaterThan:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThan:));
  203. /**
  204. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  211. */
  212. - (FIRQuery *)queryWhereField:(NSString *)field
  213. isGreaterThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThanOrEqualTo:));
  214. /**
  215. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  222. */
  223. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  224. isGreaterThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThanOrEqualTo:));
  225. /**
  226. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  235. */
  236. - (FIRQuery *)queryWhereField:(NSString *)field
  237. arrayContains:(id)value NS_SWIFT_NAME(whereField(_:arrayContains:));
  238. /**
  239. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  248. */
  249. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  250. arrayContains:(id)value NS_SWIFT_NAME(whereField(_:arrayContains:));
  251. /**
  252. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  263. */
  264. - (FIRQuery *)queryWhereField:(NSString *)field
  265. arrayContainsAny:(NSArray<id> *)values
  266. NS_SWIFT_NAME(whereField(_:arrayContainsAny:));
  267. /**
  268. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  279. */
  280. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  281. arrayContainsAny:(NSArray<id> *)values
  282. NS_SWIFT_NAME(whereField(_:arrayContainsAny:));
  283. /**
  284. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  294. */
  295. - (FIRQuery *)queryWhereField:(NSString *)field
  296. in:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:in:));
  297. /**
  298. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  308. */
  309. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  310. in:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:in:));
  311. /**
  312. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  325. */
  326. - (FIRQuery *)queryWhereField:(NSString *)field
  327. notIn:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:notIn:));
  328. /**
  329. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  342. */
  343. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  344. notIn:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:notIn:));
  345. /**
  346. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  353. */
  354. - (FIRQuery *)queryFilteredUsingPredicate:(NSPredicate *)predicate NS_SWIFT_NAME(filter(using:));
  355. #pragma mark - Sorting Data
  356. /**
  357. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field.
  358. *
  359. * @param field The field to sort by.
  360. *
  361. * @return The created `FIRQuery`.
  362. */
  363. - (FIRQuery *)queryOrderedByField:(NSString *)field NS_SWIFT_NAME(order(by:));
  364. /**
  365. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field.
  366. *
  367. * @param path The field to sort by.
  368. *
  369. * @return The created `FIRQuery`.
  370. */
  371. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)path NS_SWIFT_NAME(order(by:));
  372. /**
  373. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  380. */
  381. - (FIRQuery *)queryOrderedByField:(NSString *)field
  382. descending:(BOOL)descending NS_SWIFT_NAME(order(by:descending:));
  383. /**
  384. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  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 `FIRQuery` 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 `FIRQuery`.
  402. */
  403. - (FIRQuery *)queryLimitedTo:(NSInteger)limit NS_SWIFT_NAME(limit(to:));
  404. /**
  405. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  413. */
  414. - (FIRQuery *)queryLimitedToLast:(NSInteger)limit NS_SWIFT_NAME(limit(toLast:));
  415. #pragma mark - Choosing Endpoints
  416. /**
  417. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  424. */
  425. - (FIRQuery *)queryStartingAtDocument:(FIRDocumentSnapshot *)document
  426. NS_SWIFT_NAME(start(atDocument:));
  427. /**
  428. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  435. */
  436. - (FIRQuery *)queryStartingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(at:));
  437. /**
  438. * Creates and returns a new `FIRQuery` 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 `FIRQuery`.
  445. */
  446. - (FIRQuery *)queryStartingAfterDocument:(FIRDocumentSnapshot *)document
  447. NS_SWIFT_NAME(start(afterDocument:));
  448. /**
  449. * Creates and returns a new `FIRQuery` 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 order
  454. * by.
  455. *
  456. * @return The created `FIRQuery`.
  457. */
  458. - (FIRQuery *)queryStartingAfterValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(after:));
  459. /**
  460. * Creates and returns a new `FIRQuery` that ends before the provided document (exclusive). The end
  461. * position is relative to the order of the query. The document must contain all of the fields
  462. * provided in the orderBy of this query.
  463. *
  464. * @param document The snapshot of the document to end before.
  465. *
  466. * @return The created `FIRQuery`.
  467. */
  468. - (FIRQuery *)queryEndingBeforeDocument:(FIRDocumentSnapshot *)document
  469. NS_SWIFT_NAME(end(beforeDocument:));
  470. /**
  471. * Creates and returns a new `FIRQuery` that ends before the provided fields relative to the order
  472. * of the query. The order of the field values must match the order of the order by clauses of the
  473. * query.
  474. *
  475. * @param fieldValues The field values to end this query before, in order of the query's order by.
  476. *
  477. * @return The created `FIRQuery`.
  478. */
  479. - (FIRQuery *)queryEndingBeforeValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(before:));
  480. /**
  481. * Creates and returns a new `FIRQuery` that ends at the provided document (exclusive). The end
  482. * position is relative to the order of the query. The document must contain all of the fields
  483. * provided in the orderBy of this query.
  484. *
  485. * @param document The snapshot of the document to end at.
  486. *
  487. * @return The created `FIRQuery`.
  488. */
  489. - (FIRQuery *)queryEndingAtDocument:(FIRDocumentSnapshot *)document
  490. NS_SWIFT_NAME(end(atDocument:));
  491. /**
  492. * Creates and returns a new `FIRQuery` that ends at the provided fields relative to the order of
  493. * the query. The order of the field values must match the order of the order by clauses of the
  494. * query.
  495. *
  496. * @param fieldValues The field values to end this query at, in order of the query's order by.
  497. *
  498. * @return The created `FIRQuery`.
  499. */
  500. - (FIRQuery *)queryEndingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(at:));
  501. @end
  502. NS_ASSUME_NONNULL_END