简体   繁体   中英

Performing Mathematical Operations on kFirebaseServerValueTimestamp

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM