@@ -42,7 +42,7 @@ - (FIRDatabaseQuery *) getQueryWithModifiers:(NSArray *) modifiers
42
42
{
43
43
FIRDatabaseReference *rootRef = [self getRef ];
44
44
FIRDatabaseQuery *query = [rootRef queryOrderedByKey ];
45
-
45
+
46
46
for (NSString *str in modifiers) {
47
47
if ([str isEqualToString: @" orderByKey" ]) {
48
48
query = [rootRef queryOrderedByKey ];
@@ -107,7 +107,7 @@ - (FIRDatabaseQuery *) getQueryWithModifiers:(NSArray *) modifiers
107
107
}
108
108
}
109
109
}
110
-
110
+
111
111
return query;
112
112
}
113
113
@@ -141,7 +141,7 @@ - (void) removeEventHandler:(NSString *) name
141
141
{
142
142
FIRDatabaseReference *ref = [self getRef ];
143
143
int eventType = [self eventTypeFromName: name];
144
-
144
+
145
145
switch (eventType) {
146
146
case FIRDataEventTypeValue:
147
147
[ref removeObserverWithHandle: self .childValueHandler];
@@ -198,7 +198,7 @@ - (NSArray *) listenerKeys
198
198
- (int ) eventTypeFromName : (NSString *)name
199
199
{
200
200
int eventType = FIRDataEventTypeValue;
201
-
201
+
202
202
if ([name isEqualToString: DATABASE_VALUE_EVENT]) {
203
203
eventType = FIRDataEventTypeValue;
204
204
} else if ([name isEqualToString: DATABASE_CHILD_ADDED_EVENT]) {
@@ -240,7 +240,7 @@ @implementation FirestackDatabase
240
240
RCT_EXPORT_METHOD (enablePersistence:(BOOL ) enable
241
241
callback:(RCTResponseSenderBlock) callback)
242
242
{
243
-
243
+
244
244
BOOL isEnabled = [FIRDatabase database ].persistenceEnabled ;
245
245
if ( isEnabled != enable) {
246
246
[FIRDatabase database ].persistenceEnabled = enable;
@@ -267,7 +267,7 @@ @implementation FirestackDatabase
267
267
callback:(RCTResponseSenderBlock) callback)
268
268
{
269
269
FIRDatabaseReference *ref = [self getRefAtPath: path];
270
-
270
+
271
271
[ref setValue: value withCompletionBlock: ^(NSError * _Nullable error, FIRDatabaseReference * _Nonnull ref) {
272
272
if (error != nil ) {
273
273
// Error handling
@@ -286,7 +286,7 @@ @implementation FirestackDatabase
286
286
callback:(RCTResponseSenderBlock) callback)
287
287
{
288
288
FIRDatabaseReference *ref = [self getRefAtPath: path];
289
-
289
+
290
290
[ref updateChildValues: value withCompletionBlock: ^(NSError * _Nullable error, FIRDatabaseReference * _Nonnull ref) {
291
291
if (error != nil ) {
292
292
// Error handling
@@ -322,10 +322,10 @@ @implementation FirestackDatabase
322
322
callback:(RCTResponseSenderBlock) callback)
323
323
{
324
324
FIRDatabaseReference *ref = [[self getRefAtPath: path] childByAutoId ];
325
-
325
+
326
326
NSURL *url = [NSURL URLWithString: ref.URL];
327
327
NSString *newPath = [url path ];
328
-
328
+
329
329
if ([props count ] > 0 ) {
330
330
[ref setValue: props withCompletionBlock: ^(NSError * _Nullable error, FIRDatabaseReference * _Nonnull ref) {
331
331
if (error != nil ) {
@@ -370,12 +370,12 @@ @implementation FirestackDatabase
370
370
@" snapshot" : props
371
371
}];
372
372
};
373
-
373
+
374
374
id errorBlock = ^(NSError * _Nonnull error) {
375
375
NSLog (@" Error onDBEvent: %@ " , [error debugDescription ]);
376
376
[self getAndSendDatabaseError: error withPath: path];
377
377
};
378
-
378
+
379
379
int eventType = [r eventTypeFromName: eventName];
380
380
FIRDatabaseHandle handle = [query observeEventType: eventType
381
381
withBlock: withBlock
@@ -405,7 +405,7 @@ @implementation FirestackDatabase
405
405
FirestackDBReference *r = [self getDBHandle: path];
406
406
int eventType = [r eventTypeFromName: name];
407
407
FIRDatabaseQuery *ref = [r getQueryWithModifiers: modifiers];
408
-
408
+
409
409
[ref observeSingleEventOfType: eventType
410
410
withBlock: ^(FIRDataSnapshot * _Nonnull snapshot) {
411
411
NSDictionary *props = [self snapshotToDict: snapshot];
@@ -440,7 +440,7 @@ @implementation FirestackDatabase
440
440
}
441
441
442
442
// [self saveDBHandle:path dbRef:r];
443
-
443
+
444
444
callback (@[[NSNull null ], @{
445
445
@" result" : @" success" ,
446
446
@" path" : path,
@@ -454,7 +454,7 @@ @implementation FirestackDatabase
454
454
callback:(RCTResponseSenderBlock) callback)
455
455
{
456
456
FIRDatabaseReference *ref = [self getRefAtPath: path];
457
-
457
+
458
458
[ref onDisconnectSetValue: props
459
459
withCompletionBlock: ^(NSError * _Nullable error, FIRDatabaseReference * _Nonnull ref) {
460
460
if (error != nil ) {
@@ -543,73 +543,6 @@ - (FIRDatabaseReference *) getRefAtPath:(NSString *) str
543
543
return [r getRef ];
544
544
}
545
545
546
- - (FIRDatabaseQuery *) getQueryAtPathWithModifiers : (NSString *) str
547
- modifiers : (NSArray *) modifiers
548
- {
549
- FIRDatabaseReference *rootRef = [self getRefAtPath: str];
550
-
551
- FIRDatabaseQuery *query = [rootRef queryOrderedByKey ];
552
-
553
- for (NSString *str in modifiers) {
554
- if ([str isEqualToString: @" orderByKey" ]) {
555
- query = [rootRef queryOrderedByKey ];
556
- } else if ([str isEqualToString: @" orderByPriority" ]) {
557
- query = [rootRef queryOrderedByPriority ];
558
- } else if ([str isEqualToString: @" orderByValue" ]) {
559
- query = [rootRef queryOrderedByValue ];
560
- } else if ([str containsString: @" orderByChild" ]) {
561
- NSArray *args = [str componentsSeparatedByString: @" :" ];
562
- NSString *key = args[1 ];
563
- query = [rootRef queryOrderedByChild: key];
564
- } else if ([str containsString: @" limitToLast" ]) {
565
- NSArray *args = [str componentsSeparatedByString: @" :" ];
566
- NSString *key = args[1 ];
567
- NSUInteger limit = key.integerValue ;
568
- query = [query queryLimitedToLast: limit];
569
- } else if ([str containsString: @" limitToFirst" ]) {
570
- NSArray *args = [str componentsSeparatedByString: @" :" ];
571
- NSString *key = args[1 ];
572
- NSUInteger limit = key.integerValue ;
573
- query = [query queryLimitedToFirst: limit];
574
- } else if ([str containsString: @" equalTo" ]) {
575
- NSArray *args = [str componentsSeparatedByString: @" :" ];
576
- NSString *value = args[1 ];
577
- NSString *key = args[2 ];
578
-
579
- if (key == nil ) {
580
- query = [query queryEqualToValue: value];
581
- } else {
582
- query = [query queryEqualToValue: value
583
- childKey: key];
584
- }
585
- } else if ([str containsString: @" endAt" ]) {
586
- NSArray *args = [str componentsSeparatedByString: @" :" ];
587
- NSString *value = args[1 ];
588
- NSString *key = args[2 ];
589
-
590
- if (key == nil ) {
591
- query = [query queryEndingAtValue: value];
592
- } else {
593
- query = [query queryEndingAtValue: value
594
- childKey: key];
595
- }
596
- } else if ([str containsString: @" startAt" ]) {
597
- NSArray *args = [str componentsSeparatedByString: @" :" ];
598
- NSString *value = args[1 ];
599
- NSString *key = args[2 ];
600
-
601
- if (key == nil ) {
602
- query = [query queryStartingAtValue: value];
603
- } else {
604
- query = [query queryStartingAtValue: value
605
- childKey: key];
606
- }
607
- }
608
- }
609
-
610
- return query;
611
- }
612
-
613
546
// Handles
614
547
- (NSDictionary *) storedDBHandles
615
548
{
@@ -623,7 +556,7 @@ - (FirestackDBReference *) getDBHandle:(NSString *) path
623
556
{
624
557
NSDictionary *stored = [self storedDBHandles ];
625
558
FirestackDBReference *r = [stored objectForKey: path];
626
-
559
+
627
560
if (r == nil ) {
628
561
r = [[FirestackDBReference alloc ] initWithPath: path];
629
562
[self saveDBHandle: path dbRef: r];
@@ -639,15 +572,15 @@ - (void) saveDBHandle:(NSString *) path
639
572
FirestackDBReference *r = [stored objectForKey: path];
640
573
[r cleanup ];
641
574
}
642
-
575
+
643
576
[stored setObject: dbRef forKey: path];
644
577
self._DBHandles = stored;
645
578
}
646
579
647
580
- (void ) removeDBHandle : (NSString *) path
648
581
{
649
582
NSMutableDictionary *stored = [[self storedDBHandles ] mutableCopy ];
650
-
583
+
651
584
FirestackDBReference *r = [stored objectForKey: path];
652
585
if (r != nil ) {
653
586
[r cleanup ];
@@ -662,12 +595,26 @@ - (NSDictionary *) snapshotToDict:(FIRDataSnapshot *) snapshot
662
595
[dict setValue: snapshot.key forKey: @" key" ];
663
596
NSDictionary *val = snapshot.value ;
664
597
[dict setObject: val forKey: @" value" ];
665
-
598
+
599
+ // Snapshot ordering
600
+ NSMutableArray *childKeys = [NSMutableArray array ];
601
+ if (snapshot.childrenCount > 0 ) {
602
+ // Since JS does not respect object ordering of keys
603
+ // we keep a list of the keys and their ordering
604
+ // in the snapshot event
605
+ NSEnumerator *children = [snapshot children ];
606
+ FIRDataSnapshot *child;
607
+ while (child = [children nextObject ]) {
608
+ [childKeys addObject: child.key];
609
+ }
610
+ }
611
+
612
+ [dict setObject: childKeys forKey: @" childKeys" ];
666
613
[dict setValue: @(snapshot.hasChildren) forKey: @" hasChildren" ];
667
614
[dict setValue: @(snapshot.exists) forKey: @" exists" ];
668
615
[dict setValue: @(snapshot.childrenCount) forKey: @" childrenCount" ];
669
616
[dict setValue: snapshot.priority forKey: @" priority" ];
670
-
617
+
671
618
return dict;
672
619
}
673
620
@@ -683,7 +630,7 @@ - (NSDictionary *) getAndSendDatabaseError:(NSError *) error
683
630
sendJSEvent: DATABASE_ERROR_EVENT
684
631
title: DATABASE_ERROR_EVENT
685
632
props: evt];
686
-
633
+
687
634
return evt;
688
635
}
689
636
@@ -709,4 +656,4 @@ - (void) sendJSEvent:(NSString *)type
709
656
}
710
657
}
711
658
712
- @end
659
+ @end
0 commit comments