FIRQuery.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. // clang-format off
  63. - (void)getDocumentsWithSource:(FIRFirestoreSource)source
  64. completion:(FIRQuerySnapshotBlock)completion
  65. NS_SWIFT_NAME(getDocuments(source:completion:));
  66. // clang-format on
  67. /**
  68. * Attaches a listener for QuerySnapshot events.
  69. *
  70. * @param listener The listener to attach.
  71. *
  72. * @return A FIRListenerRegistration that can be used to remove this listener.
  73. */
  74. - (id<FIRListenerRegistration>)addSnapshotListener:(FIRQuerySnapshotBlock)listener
  75. NS_SWIFT_NAME(addSnapshotListener(_:));
  76. /**
  77. * Attaches a listener for QuerySnapshot events.
  78. *
  79. * @param includeMetadataChanges Whether metadata-only changes (i.e. only
  80. * `FIRDocumentSnapshot.metadata` changed) should trigger snapshot events.
  81. * @param listener The listener to attach.
  82. *
  83. * @return A FIRListenerRegistration that can be used to remove this listener.
  84. */
  85. // clang-format off
  86. - (id<FIRListenerRegistration>)
  87. addSnapshotListenerWithIncludeMetadataChanges:(BOOL)includeMetadataChanges
  88. listener:(FIRQuerySnapshotBlock)listener
  89. NS_SWIFT_NAME(addSnapshotListener(includeMetadataChanges:listener:));
  90. // clang-format on
  91. #pragma mark - Filtering Data
  92. /**
  93. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  94. * contain the specified field and the value must be equal to the specified value.
  95. *
  96. * @param field The name of the field to compare.
  97. * @param value The value the field must be equal to.
  98. *
  99. * @return The created `FIRQuery`.
  100. */
  101. // clang-format off
  102. - (FIRQuery *)queryWhereField:(NSString *)field
  103. isEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isEqualTo:));
  104. // clang-format on
  105. /**
  106. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  107. * contain the specified field and the value must be equal to the specified value.
  108. *
  109. * @param path The path of the field to compare.
  110. * @param value The value the field must be equal to.
  111. *
  112. * @return The created `FIRQuery`.
  113. */
  114. // clang-format off
  115. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  116. isEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isEqualTo:));
  117. // clang-format on
  118. /**
  119. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  120. * contain the specified field and the value must be less than the specified value.
  121. *
  122. * @param field The name of the field to compare.
  123. * @param value The value the field must be less than.
  124. *
  125. * @return The created `FIRQuery`.
  126. */
  127. // clang-format off
  128. - (FIRQuery *)queryWhereField:(NSString *)field
  129. isLessThan:(id)value NS_SWIFT_NAME(whereField(_:isLessThan:));
  130. // clang-format on
  131. /**
  132. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  133. * contain the specified field and the value must be less than the specified value.
  134. *
  135. * @param path The path of the field to compare.
  136. * @param value The value the field must be less than.
  137. *
  138. * @return The created `FIRQuery`.
  139. */
  140. // clang-format off
  141. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  142. isLessThan:(id)value NS_SWIFT_NAME(whereField(_:isLessThan:));
  143. // clang-format on
  144. /**
  145. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  146. * contain the specified field and the value must be less than or equal to the specified value.
  147. *
  148. * @param field The name of the field to compare
  149. * @param value The value the field must be less than or equal to.
  150. *
  151. * @return The created `FIRQuery`.
  152. */
  153. // clang-format off
  154. - (FIRQuery *)queryWhereField:(NSString *)field
  155. isLessThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isLessThanOrEqualTo:));
  156. // clang-format on
  157. /**
  158. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  159. * contain the specified field and the value must be less than or equal to the specified value.
  160. *
  161. * @param path The path of the field to compare
  162. * @param value The value the field must be less than or equal to.
  163. *
  164. * @return The created `FIRQuery`.
  165. */
  166. // clang-format off
  167. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  168. isLessThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isLessThanOrEqualTo:));
  169. // clang-format on
  170. /**
  171. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  172. * contain the specified field and the value must greater than the specified value.
  173. *
  174. * @param field The name of the field to compare
  175. * @param value The value the field must be greater than.
  176. *
  177. * @return The created `FIRQuery`.
  178. */
  179. // clang-format off
  180. - (FIRQuery *)queryWhereField:(NSString *)field
  181. isGreaterThan:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThan:));
  182. // clang-format on
  183. /**
  184. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  185. * contain the specified field and the value must greater than the specified value.
  186. *
  187. * @param path The path of the field to compare
  188. * @param value The value the field must be greater than.
  189. *
  190. * @return The created `FIRQuery`.
  191. */
  192. // clang-format off
  193. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  194. isGreaterThan:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThan:));
  195. // clang-format on
  196. /**
  197. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  198. * contain the specified field and the value must be greater than or equal to the specified value.
  199. *
  200. * @param field The name of the field to compare
  201. * @param value The value the field must be greater than.
  202. *
  203. * @return The created `FIRQuery`.
  204. */
  205. // clang-format off
  206. - (FIRQuery *)queryWhereField:(NSString *)field
  207. isGreaterThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThanOrEqualTo:));
  208. // clang-format on
  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. // clang-format off
  219. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  220. isGreaterThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThanOrEqualTo:));
  221. // clang-format on
  222. /**
  223. * Creates and returns a new `FIRQuery` with the additional filter that documents must contain
  224. * the specified field, it must be an array, and the array must contain the provided value.
  225. *
  226. * A query can have only one arrayContains filter.
  227. *
  228. * @param field The name of the field containing an array to search
  229. * @param value The value that must be contained in the array
  230. *
  231. * @return The created `FIRQuery`.
  232. */
  233. // clang-format off
  234. - (FIRQuery *)queryWhereField:(NSString *)field
  235. arrayContains:(id)value NS_SWIFT_NAME(whereField(_:arrayContains:));
  236. // clang-format on
  237. /**
  238. * Creates and returns a new `FIRQuery` with the additional filter that documents must contain
  239. * the specified field, it must be an array, and the array must contain the provided value.
  240. *
  241. * A query can have only one arrayContains filter.
  242. *
  243. * @param path The path of the field containing an array to search
  244. * @param value The value that must be contained in the array
  245. *
  246. * @return The created `FIRQuery`.
  247. */
  248. // clang-format off
  249. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  250. arrayContains:(id)value NS_SWIFT_NAME(whereField(_:arrayContains:));
  251. // clang-format on
  252. /**
  253. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  254. * satisfy the specified predicate.
  255. *
  256. * @param predicate The predicate the document must satisfy. Can be either comparison
  257. * or compound of comparison. In particular, block-based predicate is not supported.
  258. *
  259. * @return The created `FIRQuery`.
  260. */
  261. // clang-format off
  262. - (FIRQuery *)queryFilteredUsingPredicate:(NSPredicate *)predicate NS_SWIFT_NAME(filter(using:));
  263. // clang-format on
  264. #pragma mark - Sorting Data
  265. /**
  266. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field.
  267. *
  268. * @param field The field to sort by.
  269. *
  270. * @return The created `FIRQuery`.
  271. */
  272. - (FIRQuery *)queryOrderedByField:(NSString *)field NS_SWIFT_NAME(order(by:));
  273. /**
  274. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field.
  275. *
  276. * @param path The field to sort by.
  277. *
  278. * @return The created `FIRQuery`.
  279. */
  280. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)path NS_SWIFT_NAME(order(by:));
  281. /**
  282. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field,
  283. * optionally in descending order instead of ascending.
  284. *
  285. * @param field The field to sort by.
  286. * @param descending Whether to sort descending.
  287. *
  288. * @return The created `FIRQuery`.
  289. */
  290. // clang-format off
  291. - (FIRQuery *)queryOrderedByField:(NSString *)field
  292. descending:(BOOL)descending NS_SWIFT_NAME(order(by:descending:));
  293. // clang-format on
  294. /**
  295. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field,
  296. * optionally in descending order instead of ascending.
  297. *
  298. * @param path The field to sort by.
  299. * @param descending Whether to sort descending.
  300. *
  301. * @return The created `FIRQuery`.
  302. */
  303. // clang-format off
  304. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)path
  305. descending:(BOOL)descending NS_SWIFT_NAME(order(by:descending:));
  306. // clang-format on
  307. #pragma mark - Limiting Data
  308. /**
  309. * Creates and returns a new `FIRQuery` that's additionally limited to only return up to
  310. * the specified number of documents.
  311. *
  312. * @param limit The maximum number of items to return.
  313. *
  314. * @return The created `FIRQuery`.
  315. */
  316. - (FIRQuery *)queryLimitedTo:(NSInteger)limit NS_SWIFT_NAME(limit(to:));
  317. #pragma mark - Choosing Endpoints
  318. /**
  319. * Creates and returns a new `FIRQuery` that starts at the provided document (inclusive). The
  320. * starting position is relative to the order of the query. The document must contain all of the
  321. * fields provided in the orderBy of this query.
  322. *
  323. * @param document The snapshot of the document to start at.
  324. *
  325. * @return The created `FIRQuery`.
  326. */
  327. - (FIRQuery *)queryStartingAtDocument:(FIRDocumentSnapshot *)document
  328. NS_SWIFT_NAME(start(atDocument:));
  329. /**
  330. * Creates and returns a new `FIRQuery` that starts at the provided fields relative to the order of
  331. * the query. The order of the field values must match the order of the order by clauses of the
  332. * query.
  333. *
  334. * @param fieldValues The field values to start this query at, in order of the query's order by.
  335. *
  336. * @return The created `FIRQuery`.
  337. */
  338. - (FIRQuery *)queryStartingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(at:));
  339. /**
  340. * Creates and returns a new `FIRQuery` that starts after the provided document (exclusive). The
  341. * starting position is relative to the order of the query. The document must contain all of the
  342. * fields provided in the orderBy of this query.
  343. *
  344. * @param document The snapshot of the document to start after.
  345. *
  346. * @return The created `FIRQuery`.
  347. */
  348. - (FIRQuery *)queryStartingAfterDocument:(FIRDocumentSnapshot *)document
  349. NS_SWIFT_NAME(start(afterDocument:));
  350. /**
  351. * Creates and returns a new `FIRQuery` that starts after the provided fields relative to the order
  352. * of the query. The order of the field values must match the order of the order by clauses of the
  353. * query.
  354. *
  355. * @param fieldValues The field values to start this query after, in order of the query's order
  356. * by.
  357. *
  358. * @return The created `FIRQuery`.
  359. */
  360. - (FIRQuery *)queryStartingAfterValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(after:));
  361. /**
  362. * Creates and returns a new `FIRQuery` that ends before the provided document (exclusive). The end
  363. * position is relative to the order of the query. The document must contain all of the fields
  364. * provided in the orderBy of this query.
  365. *
  366. * @param document The snapshot of the document to end before.
  367. *
  368. * @return The created `FIRQuery`.
  369. */
  370. - (FIRQuery *)queryEndingBeforeDocument:(FIRDocumentSnapshot *)document
  371. NS_SWIFT_NAME(end(beforeDocument:));
  372. /**
  373. * Creates and returns a new `FIRQuery` that ends before the provided fields relative to the order
  374. * of the query. The order of the field values must match the order of the order by clauses of the
  375. * query.
  376. *
  377. * @param fieldValues The field values to end this query before, in order of the query's order by.
  378. *
  379. * @return The created `FIRQuery`.
  380. */
  381. - (FIRQuery *)queryEndingBeforeValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(before:));
  382. /**
  383. * Creates and returns a new `FIRQuery` that ends at the provided document (exclusive). The end
  384. * position is relative to the order of the query. The document must contain all of the fields
  385. * provided in the orderBy of this query.
  386. *
  387. * @param document The snapshot of the document to end at.
  388. *
  389. * @return The created `FIRQuery`.
  390. */
  391. - (FIRQuery *)queryEndingAtDocument:(FIRDocumentSnapshot *)document NS_SWIFT_NAME(end(atDocument:));
  392. /**
  393. * Creates and returns a new `FIRQuery` that ends at the provided fields relative to the order of
  394. * the query. The order of the field values must match the order of the order by clauses of the
  395. * query.
  396. *
  397. * @param fieldValues The field values to end this query at, in order of the query's order by.
  398. *
  399. * @return The created `FIRQuery`.
  400. */
  401. - (FIRQuery *)queryEndingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(at:));
  402. @end
  403. NS_ASSUME_NONNULL_END