AtulParmar
AtulParmar

Reputation: 4570

Swift 3.0, Firebase: How to get last added record

How to get last added record from "history", in history we are adding key as string like "02112017" (date: ddMMyyyy).

Way we are adding history key as string of day(02112017)?, because of if we will need to show history based on a specific day for example 31/Oct/2017 then just we find key like "31102017".

Now our problem is how to get last added record.

We can not get using queryLimited(toLast: 1) of queryLimited(toFirst: 1), it will get wrong result.

let ref = "<--history path-->"
ref.queryLimited(toLast: 1).observeSingleEvent(of: .childAdded, with: { (snapshot) in
                print(snapshot);
            });

enter image description here

enter image description here

Upvotes: 0

Views: 984

Answers (1)

Scott Kronheim
Scott Kronheim

Reputation: 772

You can store your date in the form YYYYMMDD, it is then sortable in date order. For example, represent November 2 2017 as 20171102.

When you set up your query, order it by key and then limit it to the last value.

Upvotes: 3

Related Questions