FSTIntegrationTestCase.mm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * Copyright 2017 Google LLC
  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 "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  17. #import <FirebaseFirestore/FIRCollectionReference.h>
  18. #import <FirebaseFirestore/FIRDocumentChange.h>
  19. #import <FirebaseFirestore/FIRDocumentReference.h>
  20. #import <FirebaseFirestore/FIRDocumentSnapshot.h>
  21. #import <FirebaseFirestore/FIRFirestore.h>
  22. #import <FirebaseFirestore/FIRFirestoreSettings.h>
  23. #import <FirebaseFirestore/FIRQuerySnapshot.h>
  24. #import <FirebaseFirestore/FIRSnapshotMetadata.h>
  25. #import <FirebaseFirestore/FIRTransaction.h>
  26. #include <memory>
  27. #include <string>
  28. #include <utility>
  29. #import "FirebaseCore/Extension/FIRAppInternal.h"
  30. #import "FirebaseCore/Extension/FIRLogger.h"
  31. #import "FirebaseCore/Extension/FIROptionsInternal.h"
  32. #import "Firestore/Example/Tests/Util/FIRFirestore+Testing.h"
  33. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  34. #import "Firestore/Source/API/FIRAggregateQuery+Internal.h"
  35. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  36. #include "Firestore/core/src/credentials/credentials_provider.h"
  37. #include "Firestore/core/src/credentials/empty_credentials_provider.h"
  38. #include "Firestore/core/src/credentials/user.h"
  39. #include "Firestore/core/src/local/leveldb_opener.h"
  40. #include "Firestore/core/src/model/database_id.h"
  41. #include "Firestore/core/src/remote/firebase_metadata_provider_apple.h"
  42. #include "Firestore/core/src/remote/grpc_connection.h"
  43. #include "Firestore/core/src/util/async_queue.h"
  44. #include "Firestore/core/src/util/autoid.h"
  45. #include "Firestore/core/src/util/filesystem.h"
  46. #include "Firestore/core/src/util/path.h"
  47. #include "Firestore/core/src/util/string_apple.h"
  48. #include "Firestore/core/test/unit/testutil/app_testing.h"
  49. #include "Firestore/core/test/unit/testutil/async_testing.h"
  50. #include "Firestore/core/test/unit/testutil/status_testing.h"
  51. #include "absl/memory/memory.h"
  52. using firebase::firestore::core::DatabaseInfo;
  53. using firebase::firestore::credentials::CredentialChangeListener;
  54. using firebase::firestore::credentials::EmptyAppCheckCredentialsProvider;
  55. using firebase::firestore::credentials::EmptyAuthCredentialsProvider;
  56. using firebase::firestore::credentials::User;
  57. using firebase::firestore::local::LevelDbOpener;
  58. using firebase::firestore::model::DatabaseId;
  59. using firebase::firestore::remote::FirebaseMetadataProviderApple;
  60. using firebase::firestore::testutil::AppForUnitTesting;
  61. using firebase::firestore::testutil::AsyncQueueForTesting;
  62. using firebase::firestore::util::AsyncQueue;
  63. using firebase::firestore::util::CreateAutoId;
  64. using firebase::firestore::util::Filesystem;
  65. using firebase::firestore::util::MakeString;
  66. using firebase::firestore::util::Path;
  67. using firebase::firestore::util::Status;
  68. using firebase::firestore::util::StatusOr;
  69. NS_ASSUME_NONNULL_BEGIN
  70. /**
  71. * Firestore databases can be subject to a ~30s "cold start" delay if they have not been used
  72. * recently, so before any tests run we "prime" the backend.
  73. */
  74. static const double kPrimingTimeout = 45.0;
  75. static NSString *defaultProjectId;
  76. static FIRFirestoreSettings *defaultSettings;
  77. static bool runningAgainstEmulator = false;
  78. // Behaves the same as `EmptyCredentialsProvider` except it can also trigger a user
  79. // change.
  80. class FakeAuthCredentialsProvider : public EmptyAuthCredentialsProvider {
  81. public:
  82. void SetCredentialChangeListener(CredentialChangeListener<User> changeListener) override {
  83. if (changeListener) {
  84. listener_ = std::move(changeListener);
  85. listener_(User::Unauthenticated());
  86. }
  87. }
  88. void ChangeUser(NSString *new_id) {
  89. if (listener_) {
  90. listener_(firebase::firestore::credentials::User::FromUid(new_id));
  91. }
  92. }
  93. private:
  94. CredentialChangeListener<User> listener_;
  95. };
  96. @implementation FSTIntegrationTestCase {
  97. NSMutableArray<FIRFirestore *> *_firestores;
  98. std::shared_ptr<EmptyAppCheckCredentialsProvider> _fakeAppCheckCredentialsProvider;
  99. std::shared_ptr<FakeAuthCredentialsProvider> _fakeAuthCredentialsProvider;
  100. }
  101. - (void)setUp {
  102. [super setUp];
  103. LoadXCTestCaseAwait();
  104. _fakeAppCheckCredentialsProvider = std::make_shared<EmptyAppCheckCredentialsProvider>();
  105. _fakeAuthCredentialsProvider = std::make_shared<FakeAuthCredentialsProvider>();
  106. [self clearPersistenceOnce];
  107. [self primeBackend];
  108. _firestores = [NSMutableArray array];
  109. self.db = [self firestore];
  110. self.eventAccumulator = [FSTEventAccumulator accumulatorForTest:self];
  111. }
  112. - (void)tearDown {
  113. @try {
  114. for (FIRFirestore *firestore in _firestores) {
  115. [self terminateFirestore:firestore];
  116. }
  117. } @finally {
  118. _firestores = nil;
  119. [super tearDown];
  120. }
  121. }
  122. /**
  123. * Clears persistence, but only the first time. This ensures that each test
  124. * run is isolated from the last test run, but doesn't allow tests to interfere
  125. * with each other.
  126. */
  127. - (void)clearPersistenceOnce {
  128. auto *fs = Filesystem::Default();
  129. static bool clearedPersistence = false;
  130. @synchronized([FSTIntegrationTestCase class]) {
  131. if (clearedPersistence) return;
  132. DatabaseInfo dbInfo;
  133. LevelDbOpener opener(dbInfo);
  134. StatusOr<Path> maybeLevelDBDir = opener.FirestoreAppDataDir();
  135. ASSERT_OK(maybeLevelDBDir.status());
  136. Path levelDBDir = std::move(maybeLevelDBDir).ValueOrDie();
  137. Status status = fs->RecursivelyRemove(levelDBDir);
  138. ASSERT_OK(status);
  139. clearedPersistence = true;
  140. }
  141. }
  142. - (FIRFirestore *)firestore {
  143. return [self firestoreWithProjectID:[FSTIntegrationTestCase projectID]];
  144. }
  145. /**
  146. * Figures out what kind of testing environment we're using, and sets up testing defaults to make
  147. * that work.
  148. *
  149. * Several configurations are supported:
  150. * * Mobile Harness, running periocally against prod and nightly, using live SSL certs
  151. * * Firestore emulator, running on localhost, with SSL disabled
  152. *
  153. * See Firestore/README.md for detailed setup instructions or comments below for which specific
  154. * values trigger which configurations.
  155. */
  156. + (void)setUpDefaults {
  157. if (defaultSettings) return;
  158. defaultSettings = [[FIRFirestoreSettings alloc] init];
  159. defaultSettings.persistenceEnabled = YES;
  160. // Check for a MobileHarness configuration, running against nightly or prod, which have live
  161. // SSL certs.
  162. NSString *project = [[NSProcessInfo processInfo] environment][@"PROJECT_ID"];
  163. NSString *host = [[NSProcessInfo processInfo] environment][@"DATASTORE_HOST"];
  164. if (project && host) {
  165. defaultProjectId = project;
  166. defaultSettings.host = host;
  167. NSLog(@"Integration tests running against %@/%@", defaultSettings.host, defaultProjectId);
  168. return;
  169. }
  170. // Check for configuration of a prod project via GoogleServices-Info.plist.
  171. FIROptions *options = [FIROptions defaultOptions];
  172. if (options && ![options.projectID isEqualToString:@"abc-xyz-123"]) {
  173. defaultProjectId = options.projectID;
  174. if (host) {
  175. // Allow access to nightly or other hosts via this mechanism too.
  176. defaultSettings.host = host;
  177. }
  178. NSLog(@"Integration tests running against %@/%@", defaultSettings.host, defaultProjectId);
  179. return;
  180. }
  181. // Otherwise fall back on assuming the emulator or localhost.
  182. defaultProjectId = @"test-db";
  183. defaultSettings.host = @"localhost:8080";
  184. defaultSettings.sslEnabled = false;
  185. runningAgainstEmulator = true;
  186. NSLog(@"Integration tests running against the emulator at %@/%@", defaultSettings.host,
  187. defaultProjectId);
  188. }
  189. + (NSString *)projectID {
  190. if (!defaultProjectId) {
  191. [self setUpDefaults];
  192. }
  193. return defaultProjectId;
  194. }
  195. + (bool)isRunningAgainstEmulator {
  196. // The only way to determine whether or not we're running against the emulator is to figure out
  197. // which testing environment we're using. Essentially `setUpDefaults` determines
  198. // `runningAgainstEmulator` as a side effect.
  199. if (!defaultProjectId) {
  200. [self setUpDefaults];
  201. }
  202. return runningAgainstEmulator;
  203. }
  204. + (FIRFirestoreSettings *)settings {
  205. [self setUpDefaults];
  206. return defaultSettings;
  207. }
  208. - (FIRFirestore *)firestoreWithProjectID:(NSString *)projectID {
  209. FIRApp *app = AppForUnitTesting(MakeString(projectID));
  210. return [self firestoreWithApp:app];
  211. }
  212. - (FIRFirestore *)firestoreWithApp:(FIRApp *)app {
  213. NSString *persistenceKey = [NSString stringWithFormat:@"db%lu", (unsigned long)_firestores.count];
  214. FIRSetLoggerLevel(FIRLoggerLevelDebug);
  215. std::string projectID = MakeString(app.options.projectID);
  216. FIRFirestore *firestore =
  217. [[FIRFirestore alloc] initWithDatabaseID:DatabaseId(projectID)
  218. persistenceKey:MakeString(persistenceKey)
  219. authCredentialsProvider:_fakeAuthCredentialsProvider
  220. appCheckCredentialsProvider:_fakeAppCheckCredentialsProvider
  221. workerQueue:AsyncQueueForTesting()
  222. firebaseMetadataProvider:absl::make_unique<FirebaseMetadataProviderApple>(app)
  223. firebaseApp:app
  224. instanceRegistry:nil];
  225. firestore.settings = [FSTIntegrationTestCase settings];
  226. [_firestores addObject:firestore];
  227. return firestore;
  228. }
  229. - (void)triggerUserChangeWithUid:(NSString *)uid {
  230. _fakeAuthCredentialsProvider->ChangeUser(uid);
  231. }
  232. - (void)primeBackend {
  233. static dispatch_once_t onceToken;
  234. dispatch_once(&onceToken, ^{
  235. [FSTIntegrationTestCase setUpDefaults];
  236. if (runningAgainstEmulator) {
  237. // Priming not required against the emulator.
  238. return;
  239. }
  240. FIRFirestore *db = [self firestore];
  241. XCTestExpectation *watchInitialized =
  242. [self expectationWithDescription:@"Prime backend: Watch initialized"];
  243. __block XCTestExpectation *watchUpdateReceived;
  244. FIRDocumentReference *docRef = [db documentWithPath:[self documentPath]];
  245. id<FIRListenerRegistration> listenerRegistration =
  246. [docRef addSnapshotListener:^(FIRDocumentSnapshot *snapshot, NSError *) {
  247. if ([snapshot[@"value"] isEqual:@"done"]) {
  248. [watchUpdateReceived fulfill];
  249. } else {
  250. [watchInitialized fulfill];
  251. }
  252. }];
  253. // Wait for watch to initialize and deliver first event.
  254. [self awaitExpectation:watchInitialized];
  255. watchUpdateReceived = [self expectationWithDescription:@"Prime backend: Watch update received"];
  256. // Use a transaction to perform a write without triggering any local events.
  257. [docRef.firestore
  258. runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **) {
  259. [transaction setData:@{@"value" : @"done"} forDocument:docRef];
  260. return nil;
  261. }
  262. completion:^(id, NSError *){
  263. }];
  264. // Wait to see the write on the watch stream.
  265. [self waitForExpectationsWithTimeout:kPrimingTimeout
  266. handler:^(NSError *_Nullable expectationError) {
  267. if (expectationError) {
  268. XCTFail(@"Error waiting for prime backend: %@",
  269. expectationError);
  270. }
  271. }];
  272. [listenerRegistration remove];
  273. [self terminateFirestore:db];
  274. });
  275. }
  276. - (void)terminateFirestore:(FIRFirestore *)firestore {
  277. XCTestExpectation *expectation = [self expectationWithDescription:@"shutdown"];
  278. [firestore terminateWithCompletion:[self completionForExpectation:expectation]];
  279. [self awaitExpectation:expectation];
  280. }
  281. - (void)deleteApp:(FIRApp *)app {
  282. XCTestExpectation *expectation = [self expectationWithDescription:@"deleteApp"];
  283. [app deleteApp:^(BOOL completion) {
  284. XCTAssertTrue(completion);
  285. [expectation fulfill];
  286. }];
  287. [self awaitExpectation:expectation];
  288. }
  289. - (NSString *)documentPath {
  290. std::string autoId = CreateAutoId();
  291. return [NSString stringWithFormat:@"test-collection/%s", autoId.c_str()];
  292. }
  293. - (FIRDocumentReference *)documentRef {
  294. return [self.db documentWithPath:[self documentPath]];
  295. }
  296. - (FIRCollectionReference *)collectionRef {
  297. std::string autoId = CreateAutoId();
  298. NSString *collectionName = [NSString stringWithFormat:@"test-collection-%s", autoId.c_str()];
  299. return [self.db collectionWithPath:collectionName];
  300. }
  301. - (FIRCollectionReference *)collectionRefWithDocuments:
  302. (NSDictionary<NSString *, NSDictionary<NSString *, id> *> *)documents {
  303. FIRCollectionReference *collection = [self collectionRef];
  304. // Use a different instance to write the documents
  305. [self writeAllDocuments:documents
  306. toCollection:[[self firestore] collectionWithPath:collection.path]];
  307. return collection;
  308. }
  309. - (void)writeAllDocuments:(NSDictionary<NSString *, NSDictionary<NSString *, id> *> *)documents
  310. toCollection:(FIRCollectionReference *)collection {
  311. [documents enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary<NSString *, id> *value,
  312. BOOL *) {
  313. FIRDocumentReference *ref = [collection documentWithPath:key];
  314. [self writeDocumentRef:ref data:value];
  315. }];
  316. }
  317. - (void)readerAndWriterOnDocumentRef:(void (^)(FIRDocumentReference *readerRef,
  318. FIRDocumentReference *writerRef))action {
  319. FIRFirestore *reader = self.db; // for clarity
  320. FIRFirestore *writer = [self firestore];
  321. NSString *path = [self documentPath];
  322. FIRDocumentReference *readerRef = [reader documentWithPath:path];
  323. FIRDocumentReference *writerRef = [writer documentWithPath:path];
  324. action(readerRef, writerRef);
  325. }
  326. - (FIRDocumentSnapshot *)readDocumentForRef:(FIRDocumentReference *)ref {
  327. return [self readDocumentForRef:ref source:FIRFirestoreSourceDefault];
  328. }
  329. - (FIRDocumentSnapshot *)readDocumentForRef:(FIRDocumentReference *)ref
  330. source:(FIRFirestoreSource)source {
  331. __block FIRDocumentSnapshot *result;
  332. XCTestExpectation *expectation = [self expectationWithDescription:@"getData"];
  333. [ref getDocumentWithSource:source
  334. completion:^(FIRDocumentSnapshot *doc, NSError *_Nullable error) {
  335. XCTAssertNil(error);
  336. result = doc;
  337. [expectation fulfill];
  338. }];
  339. [self awaitExpectation:expectation];
  340. return result;
  341. }
  342. - (FIRQuerySnapshot *)readDocumentSetForRef:(FIRQuery *)query {
  343. return [self readDocumentSetForRef:query source:FIRFirestoreSourceDefault];
  344. }
  345. - (FIRQuerySnapshot *)readDocumentSetForRef:(FIRQuery *)query source:(FIRFirestoreSource)source {
  346. if (query == nil) {
  347. XCTFail("Trying to read data from a nil query");
  348. }
  349. __block FIRQuerySnapshot *result;
  350. XCTestExpectation *expectation = [self expectationWithDescription:@"getData"];
  351. [query getDocumentsWithSource:source
  352. completion:^(FIRQuerySnapshot *documentSet, NSError *error) {
  353. XCTAssertNil(error);
  354. result = documentSet;
  355. [expectation fulfill];
  356. }];
  357. [self awaitExpectation:expectation];
  358. return result;
  359. }
  360. - (FIRDocumentSnapshot *)readSnapshotForRef:(FIRDocumentReference *)ref
  361. requireOnline:(BOOL)requireOnline {
  362. __block FIRDocumentSnapshot *result;
  363. XCTestExpectation *expectation = [self expectationWithDescription:@"listener"];
  364. id<FIRListenerRegistration> listener = [ref
  365. addSnapshotListenerWithIncludeMetadataChanges:YES
  366. listener:^(FIRDocumentSnapshot *snapshot,
  367. NSError *error) {
  368. XCTAssertNil(error);
  369. if (!requireOnline || !snapshot.metadata.fromCache) {
  370. result = snapshot;
  371. [expectation fulfill];
  372. }
  373. }];
  374. [self awaitExpectation:expectation];
  375. [listener remove];
  376. return result;
  377. }
  378. - (FIRAggregateQuerySnapshot *)readSnapshotForAggregate:(FIRAggregateQuery *)query {
  379. __block FIRAggregateQuerySnapshot *result;
  380. XCTestExpectation *expectation = [self expectationWithDescription:@"aggregate result"];
  381. [query aggregationWithSource:FIRAggregateSourceServer
  382. completion:^(FIRAggregateQuerySnapshot *snapshot, NSError *error) {
  383. XCTAssertNil(error);
  384. result = snapshot;
  385. [expectation fulfill];
  386. }];
  387. [self awaitExpectation:expectation];
  388. return result;
  389. }
  390. - (void)writeDocumentRef:(FIRDocumentReference *)ref data:(NSDictionary<NSString *, id> *)data {
  391. XCTestExpectation *expectation = [self expectationWithDescription:@"setData"];
  392. [ref setData:data completion:[self completionForExpectation:expectation]];
  393. [self awaitExpectation:expectation];
  394. }
  395. - (void)updateDocumentRef:(FIRDocumentReference *)ref data:(NSDictionary<id, id> *)data {
  396. XCTestExpectation *expectation = [self expectationWithDescription:@"updateData"];
  397. [ref updateData:data completion:[self completionForExpectation:expectation]];
  398. [self awaitExpectation:expectation];
  399. }
  400. - (void)deleteDocumentRef:(FIRDocumentReference *)ref {
  401. XCTestExpectation *expectation = [self expectationWithDescription:@"deleteDocument"];
  402. [ref deleteDocumentWithCompletion:[self completionForExpectation:expectation]];
  403. [self awaitExpectation:expectation];
  404. }
  405. - (FIRDocumentReference *)addDocumentRef:(FIRCollectionReference *)ref
  406. data:(NSDictionary<NSString *, id> *)data {
  407. XCTestExpectation *expectation = [self expectationWithDescription:@"addDocument"];
  408. FIRDocumentReference *doc = [ref addDocumentWithData:data
  409. completion:[self completionForExpectation:expectation]];
  410. [self awaitExpectation:expectation];
  411. return doc;
  412. }
  413. - (void)mergeDocumentRef:(FIRDocumentReference *)ref data:(NSDictionary<NSString *, id> *)data {
  414. XCTestExpectation *expectation = [self expectationWithDescription:@"setDataWithMerge"];
  415. [ref setData:data merge:YES completion:[self completionForExpectation:expectation]];
  416. [self awaitExpectation:expectation];
  417. }
  418. - (void)mergeDocumentRef:(FIRDocumentReference *)ref
  419. data:(NSDictionary<NSString *, id> *)data
  420. fields:(NSArray<id> *)fields {
  421. XCTestExpectation *expectation = [self expectationWithDescription:@"setDataWithMerge"];
  422. [ref setData:data mergeFields:fields completion:[self completionForExpectation:expectation]];
  423. [self awaitExpectation:expectation];
  424. }
  425. - (void)disableNetwork {
  426. XCTestExpectation *expectation = [self expectationWithDescription:@"disableNetwork"];
  427. [self.db disableNetworkWithCompletion:[self completionForExpectation:expectation]];
  428. [self awaitExpectation:expectation];
  429. }
  430. - (void)enableNetwork {
  431. XCTestExpectation *expectation = [self expectationWithDescription:@"enableNetwork"];
  432. [self.db enableNetworkWithCompletion:[self completionForExpectation:expectation]];
  433. [self awaitExpectation:expectation];
  434. }
  435. - (const std::shared_ptr<AsyncQueue> &)queueForFirestore:(FIRFirestore *)firestore {
  436. return [firestore workerQueue];
  437. }
  438. - (void)waitUntil:(BOOL (^)())predicate {
  439. NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
  440. double waitSeconds = [self defaultExpectationWaitSeconds];
  441. while (!predicate() && ([NSDate timeIntervalSinceReferenceDate] - start < waitSeconds)) {
  442. // This waits for the next event or until the 100ms timeout is reached
  443. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  444. beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  445. }
  446. if (!predicate()) {
  447. XCTFail(@"Timeout");
  448. }
  449. }
  450. extern "C" NSArray<NSDictionary<NSString *, id> *> *FIRQuerySnapshotGetData(
  451. FIRQuerySnapshot *docs) {
  452. NSMutableArray<NSDictionary<NSString *, id> *> *result = [NSMutableArray array];
  453. for (FIRDocumentSnapshot *doc in docs.documents) {
  454. [result addObject:doc.data];
  455. }
  456. return result;
  457. }
  458. extern "C" NSArray<NSString *> *FIRQuerySnapshotGetIDs(FIRQuerySnapshot *docs) {
  459. NSMutableArray<NSString *> *result = [NSMutableArray array];
  460. for (FIRDocumentSnapshot *doc in docs.documents) {
  461. [result addObject:doc.documentID];
  462. }
  463. return result;
  464. }
  465. extern "C" NSArray<NSArray<id> *> *FIRQuerySnapshotGetDocChangesData(FIRQuerySnapshot *docs) {
  466. NSMutableArray<NSMutableArray<id> *> *result = [NSMutableArray array];
  467. for (FIRDocumentChange *docChange in docs.documentChanges) {
  468. NSMutableArray<id> *docChangeData = [NSMutableArray array];
  469. [docChangeData addObject:@(docChange.type)];
  470. [docChangeData addObject:docChange.document.documentID];
  471. [docChangeData addObject:docChange.document.data];
  472. [result addObject:docChangeData];
  473. }
  474. return result;
  475. }
  476. @end
  477. NS_ASSUME_NONNULL_END