Skip to content

Commit

Permalink
fix(firestore, ios): fix freeze when doing a get in transactions when…
Browse files Browse the repository at this point in the history
… auth is also installed (#11773)

* fix(firestore, ios): fix an error when parsing errors in transactions

* fix(firestore, ios): fix freeze when doing a get in transactions when auth is also installed

* added tests

* revert

* revert

* useEmulator

* useEmulator

* signout

* remove auth

* revert
  • Loading branch information
Lyokone committed Oct 26, 2023
1 parent fabfe32 commit 180c091
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,45 @@ void runTransactionTests() {
await documentReference2.get();
expect(snapshot2.exists, isFalse);
});

// TODO(Lyokone): adding auth make some tests fails in macOS
// testWidgets(
// 'should not fail to complete transaction if user is authenticated',
// (_) async {
// DocumentReference<Map<String, dynamic>> doc1 =
// await initializeTest('transaction-authentified-1');

// try {
// await FirebaseAuth.instance.createUserWithEmailAndPassword(
// email: 'firestore@mail.com',
// password: 'this-is-a-password',
// );
// } catch (e) {
// await FirebaseAuth.instance.signInWithEmailAndPassword(
// email: 'firestore@mail.com',
// password: 'this-is-a-password',
// );
// }

// await doc1.set({'test': 0});

// final value = await firestore.runTransaction(
// (Transaction transaction) async {
// final value = await transaction.get(doc1);
// final newValue = value['test'] + 1;
// transaction.set(doc1, {
// 'test': newValue,
// });

// return newValue;
// },
// maxAttempts: 1,
// );

// expect(value, equals(1));

// await FirebaseAuth.instance.signOut();
// });
},
skip: kIsWeb,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,34 +621,38 @@ - (void)transactionGetApp:(nonnull FirestorePigeonFirebaseApp *)app
path:(nonnull NSString *)path
completion:(nonnull void (^)(PigeonDocumentSnapshot *_Nullable,
FlutterError *_Nullable))completion {
FIRFirestore *firestore = [self getFIRFirestoreFromAppNameFromPigeon:app];
FIRDocumentReference *document = [firestore documentWithPath:path];

FIRTransaction *transaction = self->_transactions[transactionId];

if (transaction == nil) {
completion(
nil,
[FlutterError
errorWithCode:@"missing-transaction"
message:@"An error occurred while getting the native transaction. "
@"It could be caused by a timeout in a preceding transaction operation."
details:nil]);
return;
}
// Dispatching to main thread allow us to ensure that the auth token are fetched in time
// for the transaction
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
FIRFirestore *firestore = [self getFIRFirestoreFromAppNameFromPigeon:app];
FIRDocumentReference *document = [firestore documentWithPath:path];

FIRTransaction *transaction = self->_transactions[transactionId];

if (transaction == nil) {
completion(
nil,
[FlutterError
errorWithCode:@"missing-transaction"
message:@"An error occurred while getting the native transaction. "
@"It could be caused by a timeout in a preceding transaction operation."
details:nil]);
return;
}

NSError *error = [[NSError alloc] init];
FIRDocumentSnapshot *snapshot = [transaction getDocument:document error:&error];
NSError *error = [[NSError alloc] init];
FIRDocumentSnapshot *snapshot = [transaction getDocument:document error:&error];

if (error != nil) {
completion(nil, [self convertToFlutterError:error]);
} else if (snapshot != nil) {
completion([FirestorePigeonParser toPigeonDocumentSnapshot:snapshot
serverTimestampBehavior:FIRServerTimestampBehaviorNone],
nil);
} else {
completion(nil, nil);
}
if (error != nil) {
completion(nil, [self convertToFlutterError:error]);
} else if (snapshot != nil) {
completion([FirestorePigeonParser toPigeonDocumentSnapshot:snapshot
serverTimestampBehavior:FIRServerTimestampBehaviorNone],
nil);
} else {
completion(nil, nil);
}
});
}

- (void)transactionStoreResultTransactionId:(nonnull NSString *)transactionId
Expand Down

0 comments on commit 180c091

Please sign in to comment.