@@ -22,6 +22,7 @@ @interface FirestackDBReference : NSObject
22
22
@property FIRDatabaseHandle childMovedHandler;
23
23
@property FIRDatabaseHandle childValueHandler;
24
24
+ (NSDictionary *) snapshotToDict : (FIRDataSnapshot *) snapshot ;
25
+
25
26
@end
26
27
27
28
@implementation FirestackDBReference
@@ -366,6 +367,7 @@ - (id) init
366
367
if (self != nil ) {
367
368
_dbReferences = [[NSMutableDictionary alloc ] init ];
368
369
_transactions = [[NSMutableDictionary alloc ] init ];
370
+ _transactionQueue = dispatch_queue_create (" com.fullstackreact.react-native-firestack" , DISPATCH_QUEUE_CONCURRENT);
369
371
}
370
372
return self;
371
373
}
@@ -462,24 +464,29 @@ - (id) init
462
464
applyLocally:(BOOL ) applyLocally
463
465
onComplete:(RCTResponseSenderBlock) onComplete)
464
466
{
465
- NSMutableDictionary *transactionState = [ NSMutableDictionary new ];
466
- [_transactions setValue: transactionState forKey: identifier ];
467
- dispatch_semaphore_t sema = dispatch_semaphore_create ( 0 );
468
- [transactionState setObject: sema forKey: @" semaphore " ] ;
469
-
470
- FIRDatabaseReference *ref = [ self getPathRef: path];
471
- dispatch_async ( dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
467
+ dispatch_async (_transactionQueue, ^{
468
+ NSMutableDictionary * transactionState = [ NSMutableDictionary new ];
469
+
470
+ dispatch_semaphore_t sema = dispatch_semaphore_create ( 0 ) ;
471
+ [transactionState setObject: sema forKey: @" semaphore " ];
472
+
473
+ FIRDatabaseReference *ref = [ self getPathRef: path];
472
474
[ref runTransactionBlock: ^FIRTransactionResult * _Nonnull (FIRMutableData * _Nonnull currentData) {
473
- [self sendEventWithName: DATABASE_TRANSACTION_EVENT
474
- body: @{
475
- @" id" : identifier,
476
- @" originalValue" : currentData.value
477
- }];
475
+ dispatch_barrier_async (_transactionQueue, ^{
476
+ [_transactions setValue: transactionState forKey: identifier];
477
+ [self sendEventWithName: DATABASE_TRANSACTION_EVENT
478
+ body: @{
479
+ @" id" : identifier,
480
+ @" originalValue" : currentData.value
481
+ }];
482
+ });
478
483
// Wait for the event handler to call tryCommitTransaction
479
484
dispatch_semaphore_wait (sema, DISPATCH_TIME_FOREVER);
480
485
BOOL abort = [transactionState valueForKey: @" abort" ];
481
486
id value = [transactionState valueForKey: @" value" ];
482
- [_transactions removeObjectForKey: identifier];
487
+ dispatch_barrier_async (_transactionQueue, ^{
488
+ [_transactions removeObjectForKey: identifier];
489
+ });
483
490
if (abort ) {
484
491
return [FIRTransactionResult abort ];
485
492
} else {
@@ -510,9 +517,13 @@ - (id) init
510
517
withData:(NSDictionary *) data
511
518
orAbort:(BOOL ) abort)
512
519
{
513
- NSMutableDictionary *transactionState = [_transactions valueForKey: identifier];
520
+ __block NSMutableDictionary *transactionState;
521
+ dispatch_sync (_transactionQueue, ^{
522
+ transactionState = [_transactions objectForKey: identifier];
523
+ });
514
524
if (!transactionState) {
515
525
NSLog (@" tryCommitTransaction for unknown ID %@ " , identifier);
526
+ return ;
516
527
}
517
528
dispatch_semaphore_t sema = [transactionState valueForKey: @" semaphore" ];
518
529
if (abort ) {
0 commit comments