nodebase
nodebase

Reputation: 2713

Firebase iOS query not ordering as expected

I have saved my data using childByAutoId so that it is ordered chronologically in Firebase. When I query using the query below, and then I print the snapshot.value it prints in the orders as I expected --ordered chronologically--, but when I try to enumerate over snapshot.value, and when I try to get the keys with [snap.value allkeys], they are no longer ordered chronologically. I have tried using [self.refMyOrder queryOrderedByKey] and [self.refMyOrders queryOrderedByChild:@"timestamp" for which I have a child timestamp that is a data-type long of the Epoch time. But still, it is not maintaining the desired order.

I understand that the underlying data structure of an NSDictionary is not an ordered data structure.

What am I doing wrong?

[self.refMyOrders observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
    NSLog(@"snapshot.value = %@", snapshot.value);
    NSLog(@"all keys = %@", [snapshot.value allKeys]);
}];

Upvotes: 3

Views: 917

Answers (1)

nodebase
nodebase

Reputation: 2713

So, I read the FDataSnapshot.h header file that comes with the Firebase iOS SDK, and found a way to enumerate over the snapshot so that it remains in the proper order.

[self.refMyOrders observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
    for (FDataSnapshot* child in snapshot.children) {
        NSLog(@"%@ -> %@", child.value[@"orderID"], child.value[@"timestamp"]);
    }
}];

This is exactly what I was looking for. I will leave this question here for future users. I couldn't find anything this simple.

Upvotes: 7

Related Questions