FIRQuery.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. typedef void (^FIRQuerySnapshotBlock)(FIRQuerySnapshot *_Nullable snapshot,
  25. NSError *_Nullable error);
  26. /**
  27. * A `FIRQuery` refers to a Query which you can read or listen to. You can also construct
  28. * refined `FIRQuery` objects by adding filters and ordering.
  29. */
  30. NS_SWIFT_NAME(Query)
  31. @interface FIRQuery : NSObject
  32. /** */
  33. - (id)init __attribute__((unavailable("FIRQuery cannot be created directly.")));
  34. /** The `FIRFirestore` for the Firestore database (useful for performing transactions, etc.). */
  35. @property(nonatomic, strong, readonly) FIRFirestore *firestore;
  36. #pragma mark - Retrieving Data
  37. /**
  38. * Reads the documents matching this query.
  39. *
  40. * This method attempts to provide up-to-date data when possible by waiting for
  41. * data from the server, but it may return cached data or fail if you are
  42. * offline and the server cannot be reached. See the
  43. * `getDocuments(source:completion:)` method to change this behavior.
  44. *
  45. * @param completion a block to execute once the documents have been successfully read.
  46. * documentSet will be `nil` only if error is `non-nil`.
  47. */
  48. - (void)getDocumentsWithCompletion:(FIRQuerySnapshotBlock)completion
  49. NS_SWIFT_NAME(getDocuments(completion:));
  50. /**
  51. * Reads the documents matching this query.
  52. *
  53. * @param source indicates whether the results should be fetched from the cache
  54. * only (`Source.cache`), the server only (`Source.server`), or to attempt
  55. * the server and fall back to the cache (`Source.default`).
  56. * @param completion a block to execute once the documents have been successfully read.
  57. * documentSet will be `nil` only if error is `non-nil`.
  58. */
  59. // clang-format off
  60. - (void)getDocumentsWithSource:(FIRFirestoreSource)source
  61. completion:(FIRQuerySnapshotBlock)completion
  62. NS_SWIFT_NAME(getDocuments(source:completion:));
  63. // clang-format on
  64. /**
  65. * Attaches a listener for QuerySnapshot events.
  66. *
  67. * @param listener The listener to attach.
  68. *
  69. * @return A FIRListenerRegistration that can be used to remove this listener.
  70. */
  71. - (id<FIRListenerRegistration>)addSnapshotListener:(FIRQuerySnapshotBlock)listener
  72. NS_SWIFT_NAME(addSnapshotListener(_:));
  73. /**
  74. * Attaches a listener for QuerySnapshot events.
  75. *
  76. * @param includeMetadataChanges Whether metadata-only changes (i.e. only
  77. * `FIRDocumentSnapshot.metadata` changed) should trigger snapshot events.
  78. * @param listener The listener to attach.
  79. *
  80. * @return A FIRListenerRegistration that can be used to remove this listener.
  81. */
  82. // clang-format off
  83. - (id<FIRListenerRegistration>)
  84. addSnapshotListenerWithIncludeMetadataChanges:(BOOL)includeMetadataChanges
  85. listener:(FIRQuerySnapshotBlock)listener
  86. NS_SWIFT_NAME(addSnapshotListener(includeMetadataChanges:listener:));
  87. // clang-format on
  88. #pragma mark - Filtering Data
  89. /**
  90. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  91. * contain the specified field and the value must be equal to the specified value.
  92. *
  93. * @param field The name of the field to compare.
  94. * @param value The value the field must be equal to.
  95. *
  96. * @return The created `FIRQuery`.
  97. */
  98. // clang-format off
  99. - (FIRQuery *)queryWhereField:(NSString *)field
  100. isEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isEqualTo:));
  101. // clang-format on
  102. /**
  103. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  104. * contain the specified field and the value must be equal to the specified value.
  105. *
  106. * @param path The path of the field to compare.
  107. * @param value The value the field must be equal to.
  108. *
  109. * @return The created `FIRQuery`.
  110. */
  111. // clang-format off
  112. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  113. isEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isEqualTo:));
  114. // clang-format on
  115. /**
  116. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  117. * contain the specified field and the value must be less than the specified value.
  118. *
  119. * @param field The name of the field to compare.
  120. * @param value The value the field must be less than.
  121. *
  122. * @return The created `FIRQuery`.
  123. */
  124. // clang-format off
  125. - (FIRQuery *)queryWhereField:(NSString *)field
  126. isLessThan:(id)value NS_SWIFT_NAME(whereField(_:isLessThan:));
  127. // clang-format on
  128. /**
  129. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  130. * contain the specified field and the value must be less than the specified value.
  131. *
  132. * @param path The path of the field to compare.
  133. * @param value The value the field must be less than.
  134. *
  135. * @return The created `FIRQuery`.
  136. */
  137. // clang-format off
  138. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  139. isLessThan:(id)value NS_SWIFT_NAME(whereField(_:isLessThan:));
  140. // clang-format on
  141. /**
  142. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  143. * contain the specified field and the value must be less than or equal to the specified value.
  144. *
  145. * @param field The name of the field to compare
  146. * @param value The value the field must be less than or equal to.
  147. *
  148. * @return The created `FIRQuery`.
  149. */
  150. // clang-format off
  151. - (FIRQuery *)queryWhereField:(NSString *)field
  152. isLessThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isLessThanOrEqualTo:));
  153. // clang-format on
  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 path The path 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. // clang-format off
  164. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  165. isLessThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isLessThanOrEqualTo:));
  166. // clang-format on
  167. /**
  168. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  169. * contain the specified field and the value must greater than the specified value.
  170. *
  171. * @param field The name of the field to compare
  172. * @param value The value the field must be greater than.
  173. *
  174. * @return The created `FIRQuery`.
  175. */
  176. // clang-format off
  177. - (FIRQuery *)queryWhereField:(NSString *)field
  178. isGreaterThan:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThan:));
  179. // clang-format on
  180. /**
  181. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  182. * contain the specified field and the value must greater than the specified value.
  183. *
  184. * @param path The path of the field to compare
  185. * @param value The value the field must be greater than.
  186. *
  187. * @return The created `FIRQuery`.
  188. */
  189. // clang-format off
  190. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  191. isGreaterThan:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThan:));
  192. // clang-format on
  193. /**
  194. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  195. * contain the specified field and the value must be greater than or equal to the specified value.
  196. *
  197. * @param field The name of the field to compare
  198. * @param value The value the field must be greater than.
  199. *
  200. * @return The created `FIRQuery`.
  201. */
  202. // clang-format off
  203. - (FIRQuery *)queryWhereField:(NSString *)field
  204. isGreaterThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThanOrEqualTo:));
  205. // clang-format on
  206. /**
  207. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  208. * contain the specified field and the value must be greater than or equal to the specified value.
  209. *
  210. * @param path The path of the field to compare
  211. * @param value The value the field must be greater than.
  212. *
  213. * @return The created `FIRQuery`.
  214. */
  215. // clang-format off
  216. - (FIRQuery *)queryWhereFieldPath:(FIRFieldPath *)path
  217. isGreaterThanOrEqualTo:(id)value NS_SWIFT_NAME(whereField(_:isGreaterThanOrEqualTo:));
  218. // clang-format on
  219. /**
  220. * Creates and returns a new `FIRQuery` with the additional filter that documents must
  221. * satisfy the specified predicate.
  222. *
  223. * @param predicate The predicate the document must satisfy. Can be either comparison
  224. * or compound of comparison. In particular, block-based predicate is not supported.
  225. *
  226. * @return The created `FIRQuery`.
  227. */
  228. // clang-format off
  229. - (FIRQuery *)queryFilteredUsingPredicate:(NSPredicate *)predicate NS_SWIFT_NAME(filter(using:));
  230. // clang-format on
  231. #pragma mark - Sorting Data
  232. /**
  233. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field.
  234. *
  235. * @param field The field to sort by.
  236. *
  237. * @return The created `FIRQuery`.
  238. */
  239. - (FIRQuery *)queryOrderedByField:(NSString *)field NS_SWIFT_NAME(order(by:));
  240. /**
  241. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field.
  242. *
  243. * @param path The field to sort by.
  244. *
  245. * @return The created `FIRQuery`.
  246. */
  247. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)path NS_SWIFT_NAME(order(by:));
  248. /**
  249. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field,
  250. * optionally in descending order instead of ascending.
  251. *
  252. * @param field The field to sort by.
  253. * @param descending Whether to sort descending.
  254. *
  255. * @return The created `FIRQuery`.
  256. */
  257. // clang-format off
  258. - (FIRQuery *)queryOrderedByField:(NSString *)field
  259. descending:(BOOL)descending NS_SWIFT_NAME(order(by:descending:));
  260. // clang-format on
  261. /**
  262. * Creates and returns a new `FIRQuery` that's additionally sorted by the specified field,
  263. * optionally in descending order instead of ascending.
  264. *
  265. * @param path The field to sort by.
  266. * @param descending Whether to sort descending.
  267. *
  268. * @return The created `FIRQuery`.
  269. */
  270. // clang-format off
  271. - (FIRQuery *)queryOrderedByFieldPath:(FIRFieldPath *)path
  272. descending:(BOOL)descending NS_SWIFT_NAME(order(by:descending:));
  273. // clang-format on
  274. #pragma mark - Limiting Data
  275. /**
  276. * Creates and returns a new `FIRQuery` that's additionally limited to only return up to
  277. * the specified number of documents.
  278. *
  279. * @param limit The maximum number of items to return.
  280. *
  281. * @return The created `FIRQuery`.
  282. */
  283. - (FIRQuery *)queryLimitedTo:(NSInteger)limit NS_SWIFT_NAME(limit(to:));
  284. #pragma mark - Choosing Endpoints
  285. /**
  286. * Creates and returns a new `FIRQuery` that starts at the provided document (inclusive). The
  287. * starting position is relative to the order of the query. The document must contain all of the
  288. * fields provided in the orderBy of this query.
  289. *
  290. * @param document The snapshot of the document to start at.
  291. *
  292. * @return The created `FIRQuery`.
  293. */
  294. - (FIRQuery *)queryStartingAtDocument:(FIRDocumentSnapshot *)document
  295. NS_SWIFT_NAME(start(atDocument:));
  296. /**
  297. * Creates and returns a new `FIRQuery` that starts at the provided fields relative to the order of
  298. * the query. The order of the field values must match the order of the order by clauses of the
  299. * query.
  300. *
  301. * @param fieldValues The field values to start this query at, in order of the query's order by.
  302. *
  303. * @return The created `FIRQuery`.
  304. */
  305. - (FIRQuery *)queryStartingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(at:));
  306. /**
  307. * Creates and returns a new `FIRQuery` that starts after the provided document (exclusive). The
  308. * starting position is relative to the order of the query. The document must contain all of the
  309. * fields provided in the orderBy of this query.
  310. *
  311. * @param document The snapshot of the document to start after.
  312. *
  313. * @return The created `FIRQuery`.
  314. */
  315. - (FIRQuery *)queryStartingAfterDocument:(FIRDocumentSnapshot *)document
  316. NS_SWIFT_NAME(start(afterDocument:));
  317. /**
  318. * Creates and returns a new `FIRQuery` that starts after the provided fields relative to the order
  319. * of the query. The order of the field values must match the order of the order by clauses of the
  320. * query.
  321. *
  322. * @param fieldValues The field values to start this query after, in order of the query's order
  323. * by.
  324. *
  325. * @return The created `FIRQuery`.
  326. */
  327. - (FIRQuery *)queryStartingAfterValues:(NSArray *)fieldValues NS_SWIFT_NAME(start(after:));
  328. /**
  329. * Creates and returns a new `FIRQuery` that ends before the provided document (exclusive). The end
  330. * position is relative to the order of the query. The document must contain all of the fields
  331. * provided in the orderBy of this query.
  332. *
  333. * @param document The snapshot of the document to end before.
  334. *
  335. * @return The created `FIRQuery`.
  336. */
  337. - (FIRQuery *)queryEndingBeforeDocument:(FIRDocumentSnapshot *)document
  338. NS_SWIFT_NAME(end(beforeDocument:));
  339. /**
  340. * Creates and returns a new `FIRQuery` that ends before the provided fields relative to the order
  341. * of the query. The order of the field values must match the order of the order by clauses of the
  342. * query.
  343. *
  344. * @param fieldValues The field values to end this query before, in order of the query's order by.
  345. *
  346. * @return The created `FIRQuery`.
  347. */
  348. - (FIRQuery *)queryEndingBeforeValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(before:));
  349. /**
  350. * Creates and returns a new `FIRQuery` that ends at the provided document (exclusive). The end
  351. * position is relative to the order of the query. The document must contain all of the fields
  352. * provided in the orderBy of this query.
  353. *
  354. * @param document The snapshot of the document to end at.
  355. *
  356. * @return The created `FIRQuery`.
  357. */
  358. - (FIRQuery *)queryEndingAtDocument:(FIRDocumentSnapshot *)document NS_SWIFT_NAME(end(atDocument:));
  359. /**
  360. * Creates and returns a new `FIRQuery` that ends at the provided fields relative to the order of
  361. * the query. The order of the field values must match the order of the order by clauses of the
  362. * query.
  363. *
  364. * @param fieldValues The field values to end this query at, in order of the query's order by.
  365. *
  366. * @return The created `FIRQuery`.
  367. */
  368. - (FIRQuery *)queryEndingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(at:));
  369. @end
  370. NS_ASSUME_NONNULL_END