I have JSON data coming from a Real-time Firebase Database that I want to order so that I get the trips with the lowest/highest timestamp. I'm using the queryOrdered(byChild:)
method:
class APICardService: NSObject {
class func fetchTrips(forID userID: String, completion: @escaping ([Trip]) -> Void) {
guard let userID = APIAuthService.getUserID() else { return }
let path = "users/\(userID)/trips"
print("fetch trips path", path)
Database.database().reference().child(path).queryOrdered(byChild: "timestamp").observe(.value) { (snapshot) in
var trips = [Trip]()
if let dictionary = snapshot.value as? [String: Any] {
print("DICTIONARY coming in", dictionary) // I've posted the data below
for (key, value) in dictionary {
if let dict = value as? [String: Any] {
let trip = Trip(key: key, dbValues: dict)
trips.append(trip)
}
}
}
completion(trips)
}
}
}
But objects are not coming out with the lowest/highest timestamp first (I think the query child doesn't do anything actually). Any hints?
Yes, I can use the Swift sort
built-in function but I'd prefer to just get the right query with Firebase the first time.
["-L4kDSCyapSI9ZXTKPAg": {
address = "Telangana, India";
imageURL = "https://firebasestorage.googleapis.com/v0/b/indiana-f5501.appspot.com/o/tripsImages%2FqMBqd7Xa7BW7sr8TRIsELdAjEnr1%2Ftrips_images%2FDC1F3B1D-C063-4A1B-A389-AD2271D33A26.jpg?alt=media&token=b78c9d12-f1d8-463c-bca7-6a968c60ab53";
latitude = "18.1124372";
longitude = "79.01929969999999";
placeID = "ChIJQ-0plNtQMzoRWUBZQad772M";
timestamp = 2;
title = Telangana;
}, "-L4k2GYfCa5fUSu0B5K3": {
address = "Istanbul, Turkey";
imageURL = "https://firebasestorage.googleapis.com/v0/b/indiana-f5501.appspot.com/o/tripsImages%2FqMBqd7Xa7BW7sr8TRIsELdAjEnr1%2Ftrips_images%2F4C8DA002-9F60-4178-A6E8-56150832B260.jpg?alt=media&token=368294f2-86b1-449d-8c36-69f52e6af0c3";
latitude = "41.0082376";
longitude = "28.9783589";
placeID = "ChIJawhoAASnyhQR0LABvJj-zOE";
timestamp = 4;
title = Istanbul;
}, "-L4k29yUm3k_4s0UIGN7": {
address = "Milan, Metropolitan City of Milan, Italy";
imageURL = "https://firebasestorage.googleapis.com/v0/b/indiana-f5501.appspot.com/o/tripsImages%2FqMBqd7Xa7BW7sr8TRIsELdAjEnr1%2Ftrips_images%2FD4923507-D9B2-45E2-A7DD-7E04C8786FE7.jpg?alt=media&token=ccf4a972-733c-40d0-87e9-2d557e6fdfe0";
latitude = "45.4642035";
longitude = "9.189982000000001";
placeID = "ChIJ53USP0nBhkcRjQ50xhPN_zw";
timestamp = 5;
title = Milan;
}, "-L4k2DV1wy34cLRRMyBz": {
address = "San Francisco, CA, USA";
imageURL = "https://firebasestorage.googleapis.com/v0/b/indiana-f5501.appspot.com/o/tripsImages%2FqMBqd7Xa7BW7sr8TRIsELdAjEnr1%2Ftrips_images%2F9C624C94-16D1-49BC-A27E-126FFD25DF41.jpg?alt=media&token=78cb4e15-12b2-403a-b0a5-ec2b997612a1";
latitude = "37.7749295";
longitude = "-122.4194155";
placeID = "ChIJIQBpAG2ahYAR_6128GcTUEo";
timestamp = 4000;
title = "San Francisco";
}, "-L4kBUuRlsaUm3Eve7K5": {
address = "27100 Pavia, Province of Pavia, Italy";
imageURL = "https://firebasestorage.googleapis.com/v0/b/indiana-f5501.appspot.com/o/tripsImages%2FqMBqd7Xa7BW7sr8TRIsELdAjEnr1%2Ftrips_images%2F70F95915-59CD-4E30-939D-3D954598CE6A.jpg?alt=media&token=54af1b0e-638c-4082-9383-525e3acb505a";
latitude = "45.1847248";
longitude = "9.1582069";
placeID = ChIJSQD6dUwmh0cRjoLPs7y0tfU;
timestamp = 1;
title = Pavia;
}]