1

In one of the comments on this post: firebaseArray descending order?, a Firebase employee suggests multiplying the timestamp by -1 in order to sort in descending order by date.

This is an idea that I'm trying to implement, however, I'm having a hard time doing it because kFirebaseServerValueTimestamp is a NSDictionary with the key ".sv" and a value of "timeStamp".

When I try to isolate the value, all I can get is an NSString that says "timeStamp".

Is there a way to perform mathematical operations on kFirebaseServerValueTimestamp?

Community
  • 1
  • 1
Philip Sopher
  • 627
  • 2
  • 8
  • 19

1 Answers1

2

The kFirebaseServerValueTimestamp is a property that is really used as a placeholder that firebase fills in when writing data.

From the docs:

Server Values - Placeholder values you may write into a Firebase database as a value or priority that will automatically be populated before writing to the Firebase database.

kFirebaseServerValueTimestamp - The number of milliseconds since the Unix epoch

So for example, if you wanted to base your timestamps on the Firebase server time, then

Firebase *timeStampsRef = [self.myRootRef childByAutoId];

[timeStampsRef setValue:kFirebaseServerValueTimestamp];

Firebase *timeStampsRef2 = [self.myRootRef childByAutoId];

[timeStampsRef2 setValue:kFirebaseServerValueTimestamp];

Would result in the following

-K861VyNBisoAL14dHsk: 1452890788194
-K861VyNBisoAL14dHsl: 1452890788195

If you want to sort Firebase data you should include a timestamp property as a child of the nodes you want to sort.

pizza_time
  node_id_0
   timeStamp: 20160109153000
   location: "Pizza House"
  node_id_1
   timeStamp: 20160110153000
   location: "Pizza Place"
  node_id_2
   timeStamp: 20160114153000
   location: "Pizza Busters"

You can then sort by date, query nodes from one date to another or a variety of other data acrobatics.

Jay
  • 34,438
  • 18
  • 52
  • 81