0

I can't figure out why my data won't sort, I've read through the Firebase data sorting and I'm applying a query filter as shown below, but my data is not sorting correctly.

I want the data sorted based on when it was written into firebase and from my understanding I can do that based off the ChildAutoID key. So I'm using the queryOrderedByKey. It's sorting the data in a reverse order, most recent being at the bottom of my UITableView.

Please help!

Here is the structure of the data in Firebase, it's very basic. Firebase Data Image

-(void)loadDataFromFirebase
{
    NSLog(@"LOAD DATA");
    [self removeObservers:self];
    Firebase* listRef = [[Firebase alloc] initWithUrl:@"firebaseurl/feed"];
    [[listRef queryOrderedByKey] observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot)
     {

         NSDictionary *refNum = @{
                                  @"ref" : snapshot.ref
                                  };

         [self.objectData addObject:snapshot.value];
         [self.refData addObject:refNum];
         [self.myTable reloadData];
    }];

    [listRef observeEventType:FEventTypeChildRemoved withBlock:^(FDataSnapshot *snapshot)
     {
         [self loadDataFromFirebase];

     }];
}
2
  • So I figured out a work around. Instead of using [self.objectData addObject:snapshot.value]; and adding each item at the end of the array. I switched it to [self.objectData insertObject: snapshot.value atIndex:0]; Not a great solution, but it works. I'd still like to know why or what I'm doing wrong with the queryOrderedbyKey Commented May 3, 2015 at 1:49
  • 1
    queryOrderedByKey should indeed fire FEventTypeChildAdded events in the order of the key. Unfortunately your code doesn't show enough data or information to determine what is going on. Can you set up a Firebase (e.g. 30009237.firebaseio-demo.com) with some sample data where the above code returns them in the wrong order? Commented May 3, 2015 at 2:14

1 Answer 1

1

Another option would be to load all of the nodes children from Firebase, insert them into an array then sort the array. Finally update the tableview. In the code posted above, if you have 1000 children in the node, the tableView will be refreshed 1000 times (once for each child) which is probably unnecessary. (the code below is untested, just off the top of my head).

[firebaseNode observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {

    for ( FDataSnapshot *child in snapshot.children) {

          NSDictionary *dict = child.value; //or craft an object instead of dict

          [self.myMutableArray addObject:dict];
    }

    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO]; //sort by date key, descending
    NSArray *arrayOfDescriptors = [NSArray arrayWithObject:sortDescriptor];

    [self.myMutableArray sortUsingDescriptors: arrayOfDescriptors];

    [self.tableView reloadData];    

}];

You could also filter the returned content in the for loop and only add items to the array you are interested in. A lot of this depends on the number of children being dealt with.

Sign up to request clarification or add additional context in comments.

4 Comments

any idea why queryOrderedByKey is not sorting?
@RodrigoRuiz Where do you mean? The idea of my answer was to load all of the children by .Value and sort by date in code, so the queryOrderedByKey would not be relevant. Perhaps you have a question about how to use queryOrderedByKey? If so, post a question!
I know, I just wanted to know if you knew why queryOrderedByKey wasn't working, since it would have been a better asnwer, because you wouldn't waste resources sorting it in the device.
@RodrigoRuiz Ah. The OP wanted it sorted by date, not key, which is where one issue was. Also he wanted it in reverse order so the newest was at the top. And the tableView was being refreshed with every child. I wanted to provide an alternative solution as long as the dataset size is reasonable. .Value loads everything in the node so if there are 70,000 child nodes that's obviously not a good solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.