FIRQuery.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 must be equal to 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. isEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isEqualTo:));
  110. /**
  111. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  112. * contain the specified field and the value must be less than the specified value.
  113. *
  114. * @param field The name of the field to compare.
  115. * @param value The value the field must be less than.
  116. *
  117. * @return The created `FIRQuery`.
  118. */
  119. - (FIRQuery *)queryWhereField:(NSString *)field
  120. isLessThan:(id)value NS_SWIFT_NAME(whereField(_:isLessThan:));
  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 less than the specified value.
  124. *
  125. * @param path The path of the field to compare.
  126. * @param value The value the field must be less than.
  127. *
  128. * @return The created `FIRQuery`.
  129. */
  130. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  131. isLessThan:(id)value NS_SWIFT_NAME(whereField(_:isLessThan:));
  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 or equal to 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 or equal to.
  138. *
  139. * @return The created `FIRQuery`.
  140. */
  141. - (FIRQuery *)queryWhereField:(NSString *)field
  142. isLessThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isLessThanOrEqualTo:));
  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 or equal to 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 or equal to.
  149. *
  150. * @return The created `FIRQuery`.
  151. */
  152. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  153. isLessThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isLessThanOrEqualTo:));
  154. /**
  155. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  156. * contain the specified field and the value must greater than the specified value.
  157. *
  158. * @param field The name of the field to compare
  159. * @param value The value the field must be greater than.
  160. *
  161. * @return The created `FIRQuery`.
  162. */
  163. - (FIRQuery *)queryWhereField:(NSString *)field
  164. isGreaterThan:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThan:));
  165. /**
  166. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  167. * contain the specified field and the value must greater than the specified value.
  168. *
  169. * @param path The path of the field to compare
  170. * @param value The value the field must be greater than.
  171. *
  172. * @return The created `FIRQuery`.
  173. */
  174. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  175. isGreaterThan:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThan:));
  176. /**
  177. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  178. * contain the specified field and the value must be greater than or equal to 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. isGreaterThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThanOrEqualTo:));
  187. /**
  188. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  189. * contain the specified field and the value must be greater than or equal to 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. isGreaterThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThanOrEqualTo:));
  198. /**
  199. * Creates and returns a new `FIRQuery` with the additional filter that documents must contain
  200. * the specified field, it must be an array, and the array must contain the provided value.
  201. *
  202. * A query can have only one arrayContains filter.
  203. *
  204. * @param field The name of the field containing an array to search
  205. * @param value The value that must be contained in the array
  206. *
  207. * @return The created `FIRQuery`.
  208. */
  209. - (FIRQuery *)queryWhereField:(NSString *)field
  210. arrayContains:(id)value NS_SWIFT_NAME(whereField(_:arrayContains:));
  211. /**
  212. * Creates and returns a new `FIRQuery` with the additional filter that documents must contain
  213. * the specified field, it must be an array, and the array must contain the provided value.
  214. *
  215. * A query can have only one arrayContains filter.
  216. *
  217. * @param path The path of the field containing an array to search
  218. * @param value The value that must be contained in the array
  219. *
  220. * @return The created `FIRQuery`.
  221. */
  222. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  223. arrayContains:(id)value NS_SWIFT_NAME(whereField(_:arrayContains:));
  224. /**
  225. * Creates and returns a new `FIRQuery` with the additional filter that documents must contain
  226. * the specified field, the value must be an array, and that array must contain at least one value
  227. * from the provided array.
  228. *
  229. * A query can have only one `arrayContainsAny` filter and it cannot be combined with
  230. * `arrayContains` or `in` filters.
  231. *
  232. * @param field The name of the field containing an array to search.
  233. * @param values The array that contains the values to match.
  234. *
  235. * @return The created `FIRQuery`.
  236. */
  237. - (FIRQuery *)queryWhereField:(NSString *)field
  238. arrayContainsAny:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:arrayContainsAny:));
  239. /**
  240. * Creates and returns a new `FIRQuery` with the additional filter that documents must contain
  241. * the specified field, the value must be an array, and that array must contain at least one value
  242. * from the provided array.
  243. *
  244. * A query can have only one `arrayContainsAny` filter and it cannot be combined with
  245. * `arrayContains` or `in` filters.
  246. *
  247. * @param path The path of the field containing an array to search.
  248. * @param values The array that contains the values to match.
  249. *
  250. * @return The created `FIRQuery`.
  251. */
  252. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  253. arrayContainsAny:(NSArray<id> *)values
  254. NS_SWIFT_NAME(whereField(_:arrayContainsAny:));
  255. /**
  256. * Creates and returns a new `FIRQuery` with the additional filter that documents must contain
  257. * the specified field and the value must equal one of the values from the provided array.
  258. *
  259. * A query can have only one `in` filter, and it cannot be combined with an `arrayContainsAny`
  260. * filter.
  261. *
  262. * @param field The name of the field to search.
  263. * @param values The array that contains the values to match.
  264. *
  265. * @return The created `FIRQuery`.
  266. */
  267. - (FIRQuery *)queryWhereField:(NSString *)field
  268. in:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:in:));
  269. /**
  270. * Creates and returns a new `FIRQuery` with the additional filter that documents must contain
  271. * the specified field and the value must equal one of the values from the provided array.
  272. *
  273. * A query can have only one `in` filter, and it cannot be combined with an `arrayContainsAny`
  274. * filter.
  275. *
  276. * @param path The path of the field to search.
  277. * @param values The array that contains the values to match.
  278. *
  279. * @return The created `FIRQuery`.
  280. */
  281. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  282. in:(NSArray<id> *)values NS_SWIFT_NAME(whereField(_:in:));
  283. /**
  284. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  285. * satisfy the specified predicate.
  286. *
  287. * @param predicate The predicate the document must satisfy. Can be either comparison
  288. * or compound of comparison. In particular, block-based predicate is not supported.
  289. *
  290. * @return The created `FIRQuery`.
  291. */
  292. - (FIRQuery *)queryFilteredUsingPredicate:(NSPredicate *)predicate NS_SWIFT_NAME(filter(using:));
  293. #pragma mark - Sorting Data
  294. /**
  295. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field.
  296. *
  297. * @param field The field to sort by.
  298. *
  299. * @return The created `FIRQuery`.
  300. */
  301. - (FIRQuery *)queryOrderedByField:(NSString *)field NS_SWIFT_NAME(order(by:));
  302. /**
  303. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field.
  304. *
  305. * @param path The field to sort by.
  306. *
  307. * @return The created `FIRQuery`.
  308. */
  309. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)path NS_SWIFT_NAME(order(by:));
  310. /**
  311. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field,
  312. * optionally in descending order instead of ascending.
  313. *
  314. * @param field The field to sort by.
  315. * @param descending Whether to sort descending.
  316. *
  317. * @return The created `FIRQuery`.
  318. */
  319. - (FIRQuery *)queryOrderedByField:(NSString *)field
  320. descending:(BOOL)descending NS_SWIFT_NAME(order(by:descending:));
  321. /**
  322. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field,
  323. * optionally in descending order instead of ascending.
  324. *
  325. * @param path The field to sort by.
  326. * @param descending Whether to sort descending.
  327. *
  328. * @return The created `FIRQuery`.
  329. */
  330. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)path
  331. descending:(BOOL)descending NS_SWIFT_NAME(order(by:descending:));
  332. #pragma mark - Limiting Data
  333. /**
  334. * Creates and returns a new `FIRQuery` that only returns the first matching documents up to
  335. * the specified number.
  336. *
  337. * @param limit The maximum number of items to return.
  338. *
  339. * @return The created `FIRQuery`.
  340. */
  341. - (FIRQuery *)queryLimitedTo:(NSInteger)limit NS_SWIFT_NAME(limit(to:));
  342. /**
  343. * Creates and returns a new `FIRQuery` that only returns the last matching documents up to
  344. * the specified number.
  345. *
  346. * A query with a `limit(ToLast:)` clause must have at least one `orderBy` clause.
  347. *
  348. * @param limit The maximum number of items to return.
  349. *
  350. * @return The created `FIRQuery`.
  351. */
  352. - (FIRQuery *)queryLimitedToLast:(NSInteger)limit NS_SWIFT_NAME(limit(toLast:));
  353. #pragma mark - Choosing Endpoints
  354. /**
  355. * Creates and returns a new `FIRQuery` that starts at the provided document (inclusive). The
  356. * starting position is relative to the order of the query. The document must contain all of the
  357. * fields provided in the orderBy of this query.
  358. *
  359. * @param document The snapshot of the document to start at.
  360. *
  361. * @return The created `FIRQuery`.
  362. */
  363. - (FIRQuery *)queryStartingAtDocument:(FIRDocumentSnapshot *)document
  364. NS_SWIFT_NAME(start(atDocument:));
  365. /**
  366. * Creates and returns a new `FIRQuery` that starts at the provided fields relative to the order of
  367. * the query. The order of the field values must match the order of the order by clauses of the
  368. * query.
  369. *
  370. * @param fieldValues The field values to start this query at, in order of the query's order by.
  371. *
  372. * @return The created `FIRQuery`.
  373. */
  374. - (FIRQuery *)queryStartingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(at:));
  375. /**
  376. * Creates and returns a new `FIRQuery` that starts after the provided document (exclusive). The
  377. * starting position is relative to the order of the query. The document must contain all of the
  378. * fields provided in the orderBy of this query.
  379. *
  380. * @param document The snapshot of the document to start after.
  381. *
  382. * @return The created `FIRQuery`.
  383. */
  384. - (FIRQuery *)queryStartingAfterDocument:(FIRDocumentSnapshot *)document
  385. NS_SWIFT_NAME(start(afterDocument:));
  386. /**
  387. * Creates and returns a new `FIRQuery` that starts after the provided fields relative to the order
  388. * of the query. The order of the field values must match the order of the order by clauses of the
  389. * query.
  390. *
  391. * @param fieldValues The field values to start this query after, in order of the query's order
  392. * by.
  393. *
  394. * @return The created `FIRQuery`.
  395. */
  396. - (FIRQuery *)queryStartingAfterValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(after:));
  397. /**
  398. * Creates and returns a new `FIRQuery` that ends before the provided document (exclusive). The end
  399. * position is relative to the order of the query. The document must contain all of the fields
  400. * provided in the orderBy of this query.
  401. *
  402. * @param document The snapshot of the document to end before.
  403. *
  404. * @return The created `FIRQuery`.
  405. */
  406. - (FIRQuery *)queryEndingBeforeDocument:(FIRDocumentSnapshot *)document
  407. NS_SWIFT_NAME(end(beforeDocument:));
  408. /**
  409. * Creates and returns a new `FIRQuery` that ends before the provided fields relative to the order
  410. * of the query. The order of the field values must match the order of the order by clauses of the
  411. * query.
  412. *
  413. * @param fieldValues The field values to end this query before, in order of the query's order by.
  414. *
  415. * @return The created `FIRQuery`.
  416. */
  417. - (FIRQuery *)queryEndingBeforeValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(before:));
  418. /**
  419. * Creates and returns a new `FIRQuery` that ends at the provided document (exclusive). The end
  420. * position is relative to the order of the query. The document must contain all of the fields
  421. * provided in the orderBy of this query.
  422. *
  423. * @param document The snapshot of the document to end at.
  424. *
  425. * @return The created `FIRQuery`.
  426. */
  427. - (FIRQuery *)queryEndingAtDocument:(FIRDocumentSnapshot *)document NS_SWIFT_NAME(end(atDocument:));
  428. /**
  429. * Creates and returns a new `FIRQuery` that ends at the provided fields relative to the order of
  430. * the query. The order of the field values must match the order of the order by clauses of the
  431. * query.
  432. *
  433. * @param fieldValues The field values to end this query at, in order of the query's order by.
  434. *
  435. * @return The created `FIRQuery`.
  436. */
  437. - (FIRQuery *)queryEndingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(at:));
  438. @end
  439. NS_ASSUME_NONNULL_END